如何使用正則表達式替換<*>?如何使用正則表達式將 dt[1,"A"] 的文本替換為每個 <*> 的製表符。它在第 2 行中獲得多列結果。

謝謝!
abc<AHREF>897"id="s897"</AHREF>
腳本回复:如何使用正則表達式替換<*>?忽略 0.5 秒,# 鏈接 JSL 中的註釋。 運行您自己的測試。
評論很快就會變壞,尤其是在復制/粘貼時!
回复:如何使用正則表達式替換<*>?/*
THIS IS NOT AN HTML PARSER
HTML parsers are hard to write. There are better ways.
Neither of these JSL ideas can properly skip comments or
many other HTML tags that should be skipped. They will
find things they shouldn't and miss other things they
should find. They've only been minimally tested on one site.
This code is presented as "how can I use contains() vs patmatch()
to efficiently work through a large string looking for something?"
The second example is FIVE TIMES FASTER and a LITTLE MORE ACCURATE.
The third example uses text explorer to grab links. I don't think
there is a pre-existing regex for the link text descriptions. It works
very similar to this code; it is NOT an HTML parser either.
If you can use python, you might want to investigate "beautiful soup".
I've not used it, but believe it addresses the "hard to write" issue.
*/
u = "https://community.jmp.com/t5/Discussions/bd-p/discussions";
txt = Load Text File( u );
// a link on a page has at least two parts:
// the URL and some descriptive text.
// <A ...="" href="https://community.jmp.com/url" ...="" target="_blank" rel="nofollow noopener noreferrer"> descripton </A>
// p1 p4 p5
// to parse ALL the links on a page, you'll want
// some sort of loop. There are many ways to
// write that loop; here are two choices
// using contains and regex. "<A>", p1 + 3 ); // find the end of the opening tag
p5 = Contains( txt, "</A>", p4 + 1 ); // find the ending tag
if( p5 > p4, // as long as the end is not zero, we found one, see break() below
desc = Substr( txt, p4 + 1, p5 - p4 - 1 ); // the visible link description. Images can be here too.
desc = Regex( desc, "<[^>]*>", "", globalreplace ); // remove span, image, etc tags
linktext = Substr( txt, p1, p4 - p1 + 1 ); // <A href="https://community.jmp.com/" target="_blank">
hreftext = Regex( linktext, "href\s*=\s*(\!"|')(.*?)(\1)", "\2" );// use regex to get the href value
if( !Is Missing( hreftext ), // sometimes there isn't one
dt << addrows( 1 );//
irow = N Rows( dt );//
dt:link[irow] = hreftext;//
dt:description[irow] = desc; //
);//
, // else
Break() // no more end tags. there is maybe a mess of javascript.
);//
pos = Max( pos, p5 + 3 ); // advance past the one just found
Wait( 0 );
);
stoptime = Tick Seconds();
dt << endDataUpdate;
Show( stoptime - starttime ); // .5 sec, 127 links
// using pattern matching and regex
// this is about 5X faster and skips over the garbage in the <SCRIPT> sections
// two patterns, one for &amp;amp;amp;lt;a&amp;amp;amp;gt; links and one for &amp;amp;amp;lt;script&amp;amp;amp;gt; to skip over
linkpat = "&amp;amp;amp;lt;a " + Pat Break( "&amp;amp;amp;gt;" ) &amp;amp;amp;gt;&amp;amp;amp;gt; linktext + "&amp;amp;amp;gt;" + Pat Arb() &amp;amp;amp;gt;&amp;amp;amp;gt; desctext + "&amp;amp;amp;lt;/a&amp;amp;amp;gt;";
scriptpat = "&amp;amp;amp;lt;script" + Pat Break( "&amp;amp;amp;gt;" ) + "&amp;amp;amp;gt;" + Pat Arb() + "</SCRIPT>";
dt = New Table( "patmatch", New Column( "description", Character, "Nominal" ), New Column( "link", Character, "Nominal" ) );
Wait( .1 );
dt << beginDataUpdate;
startTime = Tick Seconds();
rc = patmatch( txt,
patrepeat( // repeat the pattern until no more
(linkpat + pattest(// run some JSL for the linkpat...
desc = Regex( desctext, "<[^>]*>", "", globalreplace ); // remove span, etc tags
hreftext = Regex( linktext, "href\s*=\s*(\!"|')(.*?)(\1)", "\2" );//
if( !Is Missing( hreftext ), //
dt << addrows( 1 );//
irow = N Rows( dt );//
dt:link[irow] = hreftext;//
dt:description[irow] = desc; //
);//
1; // pattest needs this to succeed
)) | scriptpat /* maybe skip a script */ | /* maybe skip to the next tag */ (Pat Len( 1 ) + Pat Break( "<" ))
),
NULL,IGNORECASE
);
stoptime = Tick Seconds();
dt << endDataUpdate;
Show( stoptime - starttime ); // .5 sec, 128 links
Show( rc ); // should be 1, not 0
///////////////////////////////////////
// text explorer can also do this:
dt=New Table( "textexplorer",
Add Rows( 1 ),
New Column( "text", Character, "Nominal", Set Values( evallist({txt}) ) )
);
dt<<TEXT explorer=""></TEXT></A>回复:如何使用正則表達式替換<*>?我想直接通過 JMP 使用 JSL 來獲取頁面的標題和鏈接列表。
例如:這個 JMP 社區網頁,

u="https://community.jmp.com/t5/Discussions/bd-p/discussions";txt=loadtextfile(u);
t1="";offset=Contains(txt,t1);
If(offset,txt=SubStr(txt,offset+4,Length(txt)));
t2="";offset=Contains(txt,t2,-1);
If(offset,txt=SubStr(txt,1,offset-1));
txt=Substitute(txt," ","","message">","","Discussions/","Discussions/>");
txt=Substitute(txt,"<(.[^>]{0,})>","","\!n","","\!r","");
回复:如何使用正則表達式替換<*>?我不確定你是否還在問問題。 如果您仍在嘗試使某些事情起作用,那麼我需要知道您要做什麼。
您是否嘗試使用文本編輯器將文件重新格式化為 JMP 可以讀取的製表符分隔文件?
回复:如何使用正則表達式替換<*>?感謝克雷格!
我正在使用“EmEditor”的常規替換
<(.[^>]{0,})>腳本:
document.selection.Replace("<(.[^>]{0,})>","\\t",eeReplaceAll | eeFindReplaceRegExp,eeExFindSeparateCRLF);回复:如何使用正則表達式替換<*>?我最近一直在寫 C 代碼。 C 使用 \" 轉義 ",但 JSL 使用 \!" 轉義 "。
所有三個 inside-the-string " 都需要 JSL 轉義。
回复:如何使用正則表達式替換<*>?告訴你它未經測試......使用
\!”
對於逃跑的“
回复:如何使用正則表達式替換<*>?感謝克雷格!
我試了操作,JSL運行失敗。


回复:如何使用正則表達式替換<*>?我認為您需要 b、c、... 列的單獨公式
b 列的公式應該提取部分 href 字符串。
c 列的公式應該提取部分 id 字符串。
如果每行的數據形狀相同,則確切的公式可能非常簡單。 例如,如果在 b 列的 之前總是有一個數字,(下面是未經測試的代碼)
regex(a, "(\d+)", "\1")
可能會奏效。 打破這種模式:
- (\d+) - 括號創建 \1 捕獲組,\d 是一個數字,+ 是一個或多個
- - 必須跟在數字後面的文字
- "\1" - 將捕獲組 1 插入結果中
類似地,如果 c 列文本始終由 id= 標識,
regex(a,"id=\"[^\"]*\"")
可能會奏效。
此模式使用 \"三地方表示由引號括起來的 JSL 字符串中的引號。 如果我們刪除外部引號並取消轉義內部引號,則字符串正則表達式看到的是<STRONG>id="[^"]*"</STRONG>分解為
- 匹配身份證=和"
- [^"]是一個字符類,它匹配任何不是"
- *指零個或多個非- "人物
- 其次是"
像這樣的簡單解析通常適用於表現良好的數據。 在一般情況下,您可能需要更複雜的模式(如果可能有另一個 id= 字符串等)。