문제
반복되는 패턴을 가진 텍스트 문자열이 있습니다. 문자열의 일부 데이터를 데이터 테이블로 추출하려고합니다. 문자열의 각 반복은 테이블의 행을 나타냅니다. 테이블에 속하지 않으므로 건너 뛸 데이터가 많이있을 수 있습니다.
해결책
JSL 사용 패트 매치 기능. pattern을 만들기 위해서는 pat___ 함수를 사용해야 할 것이다. 다음 예제는 웹 페이지에서 발견 된 링크의 데이터 테이블을 만듭니다.
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 예제의 경우, # 기호로 시작하는 링크는 웹 페이지 내에 앵커에 불과하며 (클릭하면 페이지가 섹션으로 스크롤 됨) 코드가이를 무시합니다. 그렇지 않으면 링크가 저장되어야합니다. 저장된 링크는 /로 시작하면 사이트 앞에 붙습니다. 비슷한 세부 사항을 처리해야 할 수도 있습니다.
참고 사항
https://community.jmp.com/t5/Uncharted/Pattern-Matching/ba-p/21005