Problem
Sie haben eine Textzeichenfolge mit einem sich wiederholenden Muster. Sie möchten einige Daten aus der Zeichenfolge in eine Datentabelle extrahieren. Jede Wiederholung in der Zeichenfolge repräsentiert eine Zeile in der Tabelle. Es können viele Daten übersprungen werden, da sie nicht in die Tabelle gehören.
Lösung
Verwenden Sie die JSL PatMatch Funktion. Sie müssen die Funktionen pat___ verwenden, um ein Muster zu erstellen. Das folgende Beispiel erstellt eine Datentabelle mit Links, die auf einer Webseite gefunden werden.
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 textDatentabelle mit Links und zugehörigem Text
Diskussion
Ihr Muster wird anders sein. Die Zeichen sind Teil der HTML-Spezifikation, und der Mustervergleicher verwendet sie, um die Links im Text zu finden. Ihre Daten folgen einem anderen Muster. Sie möchten PatFence und PatTest, aber auch spezielle Fälle in PatTest ändern. In diesem HTML-Beispiel sind Verknüpfungen, die mit einem # -Zeichen beginnen, nur Anker innerhalb einer Webseite (sie lassen die Seite beim Klicken auf einen Abschnitt scrollen), sodass der Code sie ignoriert. Ansonsten sollte der Link gespeichert werden. Den gespeicherten Links wurde die Site vorangestellt, wenn sie mit einem / beginnen. Möglicherweise müssen Sie einige ähnliche Details behandeln.
Siehe auch
https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005