正規表示式:無法取得符合的字串的所有部分我正在嘗試提取字串中匹配的所有部分,但我使用正則表達式匹配,但無法獲得我想要的“BWA005,BWAZ006”。
ex="Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
Regex match( ex, "[\!"](BWA|BWAZ|BWAZ_)([0-9]{1,})(.*?)[\!"]" )
自動化和腳本編寫蘋果系統回覆:正規表示式:無法取得符合的字串的所有部分我確信這不是最好的解決方案,但 Regex Match() 似乎沒有做我想做的事情,就像 python 的 re.findall 一樣。您可以使用此原始腳本來取得所有符合項目的清單。希望有人知道更好的方法。
Names Default To Here( 1 );
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
matchlist = {};
While( !Is Missing( Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" ) ),
a = Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" );
Insert Into( matchlist, a );
Substitute Into( ex, a, "" );
);
回覆:正規表示式:無法取得符合的字串的所有部分實際上,如果您想查看其中的重複項,例如 { "BWA005", "BWAZ006", "BWA005" },您可能需要這樣做:
Names Default To Here( 1 );
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
matchlist = {};
While( !Is Missing( Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" ) ),
a = Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" );
Insert Into( matchlist, a );
b = Contains( ex, a );
c = Length( a );
ex = Substr( ex, b + c );
);回覆:正規表示式:無法取得符合的字串的所有部分謝謝 !有用!
我想知道是否有更好的方法來做到這一點。
回覆:正規表示式:無法取得符合的字串的所有部分對於 JSL 的未來維護者更好,還是對電腦更好,或者其他一些措施?這裡有一些想法;我傾向於第一個,部分原因是我想像您是從 JSL 表達式開始的,而不是文字字串。
關於評論和看到樹木與看到森林的註釋:我的評論是樹級的。它們不描述你的目標(森林),而是描述樹木。將來維護 JSL 的任何人都會想要這兩種類型。森林級別的評論應該解釋您為什麼要考慮這些值。樹級別是關於不明顯的程式碼如何運作的。
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
// write(ex) // Select Where( :"BWA"n == "BWA005" | :"BWA"n == "BWAZ006" )
// Regex match( ex, "[\!"](BWA|BWAZ|BWAZ_)([0-9]{1,})(.*?)[\!"]" )
// desired result: "BWA005,BWAZ006"
//
// it isn't 100% clear what the rules are. These examples make different assumptions.
// assume the text is valid JSL that can be parsed back into an expression
// below, also assumes | but not & and no nested parens
expression = Parse( ex ); // Select Where( :BWA == "BWA005" | :BWA == "BWAZ006" )
// get the argument of the select where(...)
expression = Arg( expression, 1 ); // :BWA == "BWA005" | :BWA == "BWAZ006"
// assume there are only | (or) operators, change x|y|z => {x,y,z}
Substitute Into( expression, Expr( Or() ), {} ); // {:BWA == "BWA005", :BWA == "BWAZ006"}
result = {}; // accumulate answer in a result list
// process the expression list
For Each( {op}, expression, // :BWA == "BWA005" etc.
// the Right Hand Side of :BWA == "BWA005" is "BWA005"
RHS = Arg( Name Expr( op ), 2 ); // assign(:BWA,"BWA005"), want 2nd arg
// apply the test to see if this is one to keep
If( !Is Missing( Regex( RHS, "(BWA|BWAZ|BWAZ_)([0-9]+)" ) ),
Insert Into( result, Arg( op, 2 ) ); // yes: keep it in the result list
);
);
// join the list of RHS strings, separated by commas
Show( Concat Items( result, "," ) ); // Concat Items(result, ",") = "BWA005,BWAZ006";
<BR />// alternate example<BR /><BR />
// assume the strings can't collide with column names, perhaps because no
// column name has a suffix number and all strings do have a suffix number
result = {};
Pat Match(
ex,
// this is the pattern; it repeats as long as it can
Pat Repeat(
// either match the BWA... pattern, in quotes, stashing the regex match into the result:
( "\!"" + Pat Regex( "(BWA|BWAZ|BWAZ_)([0-9]+)" ) >> result[N Items( result ) + 1] + "\!"" )
// >> is the patImmediate() operator that copies the LHS match into the RHS location
// the list is initially 0 items long, and [nitems+1] extends the list by one more item
| // OR the alternative...
Pat Len( 1 ) // advance one character. This is how most of the text is matched.
)
);
Show( Concat Items( result, "," ) ); // Concat Items(result, ",") = "BWA005,BWAZ006";
// you *could* write a pattern match that would parse the text as an expression,
// but that is what the first example did.回覆:正規表示式:無法取得符合的字串的所有部分對於正規表示式匹配有願望
在 Regex Match() 中新增標誌以尋找所有不重疊出現的模式。根據您嘗試執行的操作(+原因和位置),有許多不同的選項可以處理此問題(僅舉幾個已經提到的:循環和jmp 自己的模式匹配),但可能有更多選項更適合您的用例。
回覆:正規表示式:無法取得符合的字串的所有部分有了 Jmp18,使用 Python 的 re.findall 就變得容易多了:
ex="Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
Python send(ex);
Python Submit ("
import re
matches = re.findall(r'(BWA|BWAZ)([0-9]+)',ex)
");
Python get (matches)