Problema
Hai una stringa di testo con uno schema ripetuto. Si desidera estrarre alcuni dati dalla stringa in una tabella di dati. Ogni ripetizione nella stringa rappresenta una riga nella tabella. Potrebbero esserci molti dati da ignorare perché non appartengono alla tabella.
Soluzione
Usa la JSL PatMatch funzione. Avrai bisogno di usare le funzioni pat___ per costruire un pattern. L'esempio seguente crea una tabella di dati di collegamenti trovati su una pagina 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 textTabella dati di collegamenti e testo associato
Discussione
Il tuo modello sarà diverso; i caratteri fanno parte delle specifiche html e il pattern matcher li usa per trovare i collegamenti nel testo. I tuoi dati seguiranno qualche altro schema. Avrai bisogno di PatFence e PatTest, ma dovrai anche modificare casi speciali in PatTest. Per questo esempio html, i collegamenti che iniziano con un segno # sono solo ancore all'interno di una pagina Web (fanno scorrere la pagina verso una sezione quando viene cliccato) in modo che il codice li ignori. Altrimenti, il collegamento dovrebbe essere salvato. I collegamenti che vengono salvati hanno il sito anteposto se iniziano con /. Potrebbe essere necessario gestire alcuni dettagli simili.
Guarda anche
https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005