正規表示式幫助 -你好,我需要一些幫助來使用正規表示式函數--
字串就像——
“很好。2020。可選。(不)”
必填—“Fine_2020_Optional”
我嘗試了幾種組合,但無法在一個正規表示式中獲得結果。 我在 for 循環腳本中使用這種類型的正規表示式。
有什麼幫助嗎?
謝謝。
回覆:正規表示式幫助 -謝謝。 這非常有幫助回覆:正規表示式幫助 -@Craige_Hales工藤 x 10^6
謝謝
回覆:正規表示式幫助 -你好@Byron_JMP !
我可能會這樣做:
source = "(aaa).Fine.2020.Optional.(Not).more.(leave out).last.(bbb)";
//
// remove text between parens, change . to _
//
parts = {}; // accumulator list of the individual parts
ipart = 0; // index to the parts list
rc = Pat Match( // test the rc (below) to make sure the match worked
source,
patpos(0) // PatRepeat must start at the beginning.
+
Pat Repeat(
( "." | Pat Pos( 0 ) ) // advance past a required period, or begin of string
+ // followed by either...
(
( "(" + Pat Break( ")" ) + ")" )
// above: match open paren and stop on a close paren. Parens and everything
// between is rejected by not adding anything to the list of parts.
| // or
( ( Pat Break( "." ) | Pat Rem() ) >> parts[ipart += 1] )
// above: scan forward up to, but not including, a period, or if
// that doesn't work, to the end (remainder) of the string.
// ipart += 1 adds 1 before subscripting. It is OK to add an item
// to a list just beyond the end of the list. The >> operator stores
// the matched value on its left into the variable on its right.
)
)
+
patrpos(0) // PatRepeat must stop at the end.
);
// re-join the parts with the _ separator
result = if(rc,Concat Items( parts, "_" ),"problem with match");
show(result);結果=“Fine_2020_Optional_more_last”;
它相當長,而且相當不透明(也許不像正規表示式那麼不透明)。 但...
- Regex() 的真正目標是進行多次替換; GLOBALREPLACE 是高效率完成的。 您可能需要第二個正規表示式來刪除 (文字),如果有多個,也可以使用 GLOBALREPLACE。
- Word(),在 Jeff 的範例中也不錯如果您知道要保留多少個以及(文字)出現的位置。 它的簡單性和可維護性以及速度是無與倫比的。
- PatMatch() 在其他方面大放異彩;它實際上是用於提取訊息,或最多是在單一位置中修改字串(沒有 GLOBALREPLACE)。 此範例顯示 PatMatch() 提取資訊以產生零件列表,然後重新組裝零件。
另一方面,這是關於如何使用模式匹配從字串中提取資訊的精彩介紹。 真正的好處來自於可以在單一匹配中解析較長的字串(如上所述,在短字串上),因為匹配僅具有一次啟動開銷。 透過讓 PatRepeat 一次性遍歷字串,並且不修改字串,匹配速度可以相當快。
回覆:正規表示式幫助 -模式匹配也可能是一個很好的解決方案,但我總是被語法絆倒。 要是周圍有人能說流利的 snbol 就好了… @Craige_Hales
回覆:正規表示式幫助 -不要忘記用於進行字串操作的內建 JMP 函數,例如代替() ,芒格()和,我最喜歡的,單字() 。 對於這樣的事情,這些語法可能更容易處理。
str="Fine.2020.Optional.(Not)";
show(substitute(str, ".", "_"));
show(word(1, str, ".") || "_" || word(2, str, ".") || "_" || word(3, str, "."));
show(concat items(words(str, ".")[1::3], "_"));
紀錄:
Substitute(str, ".", "_") = "Fine_2020_Optional_(Not)";
Word(1, str, ".") || "_" || Word(2, str, ".") || "_" || Word(3, str, ".") = "Fine_2020_Optional";
Concat Items(Words(str, ".")[Index(1, 3)], "_") = "Fine_2020_Optional";
回覆:正規表示式幫助 -好的,所以離線時我收到了一些關於這個非常簡短的解決方案的評論。
以下是如何自行到達那裡。 這可能是作弊,但這要容易得多。
我製作了一個表格,其中包含範例。 選擇該列並使用重新編碼。
在 Recode 中,我使用了替換字串,並開啟了正規表示式。
在重新編碼對話方塊中,我開啟了兩個腳本選項(右下角)
然後將腳本保存到腳本窗口,複製一點並貼上到這裡。 (好吧,驗證它有效然後貼上它。)
非常感謝@歐內斯特帕索爾


哦,還有一點使用“\”。而不是 ”。”獨自的。 反斜線是轉義字符,不會用“_”替換所有內容。 這是完成這項工作所需的唯一一點正規表示式知識。
回覆:正規表示式幫助 -正規表示式( “這.is.jmp” , “\。” , “_” 、全域替換);
回覆:正規表示式幫助 -這確實非常有幫助。 謝謝。
回覆:正規表示式幫助 -該網站提供了一些有關正規表示式的重要一般資訊:
https://www.regular-expressions.info/quickstart.html
還有一個工具可以根據您的輸入和預期輸出產生正規表示式:
http://regex.inginf.units.it/
回覆:正規表示式幫助 -謝謝保羅,
很少有更簡單的,我能做到,但就是這個。這給我帶來了一些麻煩。 下次如果我發現任何問題,我會向您提供更多詳細資訊。 您是否有一些關於正規表示式的參考/連結可以與我/我們分享並提供很好的範例? 這將非常有幫助。
再次感謝。回覆:正規表示式幫助 -如果我們有更多輸入和預期輸出以及您遇到問題的範例,可能會有所幫助。 下面的 JSL 適用於您的單一案例,但我不確定它是否適用於您的所有資料。
Regex("Fine.2020.Optional.(Not)",
"(\w+?)\.(\d+?)\.(\w+?)\.\(\w+?\)",
"\1_\2_\3"
);