問題
繰り返しパターンを持つテキスト文字列があります。文字列からデータテーブルにデータを抽出します。文字列内の各繰り返しは、テーブル内の行を表します。テーブルに属していないため、スキップするデータが大量にある可能性があります。
溶液
JSLを使う PatMatch 関数。パターンを構築するにはpat___関数を使う必要があります。次の例では、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 textリンクと関連テキストのデータテーブル
討論
あなたのパターンは異なります。<と>の文字はhtml仕様の一部であり、パターンマッチはテキスト内のリンクを見つけるためにそれらを使用します。あなたのデータは他のパターンに従うでしょう。PatFenceとPatTestが必要ですが、PatTestの特別な場合も変更する必要があります。このHTMLの例では、#記号で始まるリンクはWebページ内の単なるアンカーであり(クリックするとページがセクションにスクロールします)、コードでは無視されます。それ以外の場合は、リンクを保存する必要があります。/で始まる場合、保存されているリンクの先頭にサイトが追加されます。あなたはいくつかの同様の詳細を扱う必要があるかもしれません。
参照
https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005