Problema
Tienes una cadena de texto que tiene un patrón de repetición. Desea extraer algunos datos de la cadena en una tabla de datos. Cada repetición en la cadena representa una fila en la tabla. Puede haber una gran cantidad de datos para omitir porque no pertenece a la tabla.
Solución
Usa el JSL PatMatch función. Tendrá que usar las funciones pat___ para construir un patrón. El siguiente ejemplo hace una tabla de datos de enlaces que se encuentran en una página web.
site = "https://en.wikipedia.org";
url = "/wiki/JMP_(statistical_software)";
html = Load Text File( site || url ); // Dec 2017: 200+ links here
quote = "\!""; // simplify escaping of quotation mark elsewhere
dt = New Table( url, New Column( "link", Character ), New Column( "text", Character ) );
// A typical link looks like this on Wikipedia:
//<A href="https://st.wikipedia.org/" lang="st" target="_blank" rel="nofollow noopener noreferrer">Sesotho</A>
// the following pattern will need tweaking for other sites.
rc = Pat Match( html, //
Pat Repeat( // until there are no more
Pat Break( "<" ) + // match up to, but not including, the <
(// either we've found a link with <A>> vlink + Pat Break( ">" ) // store match in vlink
// throw away the closing > then capture everything up to the
// closing </A>. this may include other tags
+ ">" + Pat Arb() >> vtext + "" + // store match in vtext
Pat Fence() + // fence off previously parsed data, back-up-and-retry is pointless
Pat Test( // inject some JSL into the match to save the results
If( Starts With( vlink, "#" ), // ignore in-page anchors
{} // nothing
, // else
If( Starts With( vlink, "/" ), // within site links begin with /
vlink = site || vlink; // fully qualified
);
dt << addrows( 1 ); // extend the table by one row
dt:link = vlink; // vlink and vtext are the JSL variables
dt:text = vtext; // link and text are the table variables
); //
1; // PatTest needs a true result to keep going
) //
) //
| // or we found something else and can just skip it
Pat Break( ">" ) // match up to the closing > and throw it away
) //
) //
);
Data table of links and associated textTabla de datos de enlaces y texto asociado.
Discusión
Tu patrón será diferente; los caracteres forman parte de la especificación html y el comparador de patrones los usa para encontrar los enlaces en el texto. Tus datos seguirán algún otro patrón. Querrá el PatFence y el PatTest, pero también querrá cambiar cualquier caso especial en el PatTest. Para este ejemplo html, los enlaces que comienzan con un signo # son solo anclajes dentro de una página web (hacen que la página se desplace a una sección cuando se hace clic), por lo que el código los ignora. De lo contrario, el enlace debe ser guardado. Los enlaces que se guardan tienen el sitio antepuesto si comienzan con un /. Es posible que necesite manejar algunos detalles similares.
Ver también
https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005