编写正则表达式需要帮助 -- TCL
只是寻求帮助编写一个正则表达式来匹配以下字符串集。我想编写一个与以下所有字符串匹配的表达式 TCL
i) ( XYZ XZZ XVZ XWZ )
线索:起始字符串是 X ,结束字符串对于所有对来说都是相同的。只有中间字符串与 YZV W 不同。
我的试用: [regexp {^X([Y|Z|V|W]*)Z$}]
我想编写另一个捕获/的正则表达式 我的试用:[regexp {^X([Y]*)Z$}]
或
ii) (XYZ)
简单地 regexp {^XYZ$}
code>
只是想确保它是正确的方法。有没有其他方法可以优化正则表达式:)
i)测试的第一个问题
set to_Match_Str "XYZ XZZ XVZ XWZ"
foreach {wholeStr to_Match_Str} [regexp -all -inline {X[YZVW]Z} $to_Match_Str] {
puts "MATCH $to_Match_Str in the list"
}
它仅从列表中打印 XZZ XWZ 。它省略了 XYZ 和XVZ 当我包含括号 [regexp -all -inline {X([YZVW])Z} $to_Match_Str] 时。它正确打印所有中间字符 YZVW
Just seeking a favour to write a regular expression to match the following set of strings. I want to write an expression which matches all the following strings TCL
i) ( XYZ XZZ XVZ XWZ )
Clue : Starting string is X and Z ending string is same for all the pairs. Only the middle string is differs Y Z V W.
My trial: [regexp {^X([Y|Z|V|W]*)Z$}]
I want to write an another regexp which catches/matches only the following string wherever comes
ii) (XYZ)
My trial: [regexp {^X([Y]*)Z$}]
or simply regexp {^XYZ$}
Just want to make sure its a correct approach. Is there any other way available to optimize the regexp :)
i) 1st Question Tested
set to_Match_Str "XYZ XZZ XVZ XWZ"
foreach {wholeStr to_Match_Str} [regexp -all -inline {X[YZVW]Z} $to_Match_Str] {
puts "MATCH $to_Match_Str in the list"
}
It prints only XZZ XWZ from the list. Its leaves out XYZ & XVZ
When I include the paranthesis [regexp -all -inline {X([YZVW])Z} $to_Match_Str]. It prints all the middle characters correctly Y Z V W
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您不在整个括号后面,则可以使用以下命令进行匹配:
这是因为内部字符串都是单个字符。 (它还将匹配的子字符串存储在变量
matchedSubstr
中;在那里选择您想要的任何变量名称。)您不应在[]|
code> 在正则表达式中,因为它没有特殊含义。 (您可能需要在外部添加^$
锚点。)另一方面,如果您想匹配多个字符序列(其中
Y
等只是站立) -ins for) 那么你可以使用这个:注意这里使用了
|
,但是[]
没有 。如果您的真实字符串包含许多这样的字符串(无论您使用哪种模式来匹配它们),那么提取它们的最简单方法是使用
regexp
-all -inline 选项code>,通常用在foreach
中,如下所示:根据口味混合搭配。
这对于精确比较来说是最佳的。事实上,如果这是字面意思的话,Tcl 会在内部将其优化为直字符串相等测试。
Assuming you're not after literal parentheses around the whole lot, you match that using this:
That's because the interior strings are all single characters. (It also stores the matched substring in the variable
matchedSubstr
; choose any variable name there that you want.) You should not use|
inside a[]
in a regular expression, as it has no special meaning there. (You might need to add^$
anchors round the outside.)On the other hand, if you want to match multiple character sequences (which the
Y
etc. are just stand-ins for) then you use this:Notice that
|
is being used here, but[]
is not.If your real string has many of these strings (whichever pattern you're using to match them) then the easiest way to extract them all is with the
-all -inline
options toregexp
, typically used in aforeach
like this:Mix and match to taste.
That's optimal for an exact comparison. And in fact Tcl will optimize that internally to a straight string equality test if that's literal.
这将匹配给定的字符串,但当您使用
*
乘数时,它也会匹配诸如“XZ”、“XYYYYYYYYYYYYYYYYZ”和“XYZYVWZWWWZVYYWZ”之类的字符串。要仅匹配中间字符一次,请不要使用乘数:同样,它也会匹配“XZ”,“XYYZ”和“XYYYYYYYYYYYYYYYYZ”等字符串。不要在集合后添加乘数:
这不会捕获任何内容。要使其与另一个执行相同的操作(捕获 Y 字符),您需要括号:
That would match the strings given, but as you are using the
*
multiplier it would also match strings like "XZ", "XYYYYYYYYYYYYYYYYZ" and "XYZYVWZWWWZVYYWZ". To match the middle character only once, don't use a multiplier:The same there, it will also match strings like "XZ", "XYYZ" and "XYYYYYYYYYYYYYYYYZ". Don't put a multiplier after the set:
That won't catch anything. To make it do the same as the other (catch the Y character), you need the parentheses:
您可以使用 Visual Regexp 工具来提供帮助,它在您构建正则表达式时提供反馈。
You can use the Visual Regexp tool to help, it provides feedback as you construct your regular expression.