正则表达式:无法获取匹配的字符串的所有部分我正在尝试提取字符串中匹配的所有部分,但我使用正则表达式匹配,但无法获得我想要的“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)