编写正则表达式需要帮助 -- TCL

发布于 2024-08-31 00:54:20 字数 709 浏览 3 评论 0原文

只是寻求帮助编写一个正则表达式来匹配以下字符串集。我想编写一个与以下所有字符串匹配的表达式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨后咖啡店 2024-09-07 00:54:20

i) (XYZ XZZ XVZ XWZ)

线索:起始字符串是X,结束字符串是Z,所有对都相同。只有中间字符串不同 YZV W

我的试用版:[regexp {^X([Y|Z|V|W]*)Z$}]

假设您不在整个括号后面,则可以使用以下命令进行匹配:

regexp {X([YZVW])Z} $string -> matchedSubstr

这是因为内部字符串都是单个字符。 (它还将匹配的子字符串存储在变量 matchedSubstr 中;在那里选择您想要的任何变量名称。)您不应在 []| code> 在正则表达式中,因为它没有特殊含义。 (您可能需要在外部添加 ^$ 锚点。)

另一方面,如果您想匹配多个字符序列(其中 Y 等只是站立) -ins for) 那么你可以使用这个:

regexp {X(Y|Z|V|W)Z} $string -> matchedSubstr

注意这里使用了 | ,但是 [] 没有

如果您的真实字符串包含许多这样的字符串(无论您使用哪种模式来匹配它们),那么提取它们的最简单方法是使用 regexp-all -inline 选项code>,通常用在 foreach 中,如下所示:

foreach {wholeStr matchedSubstr} [regexp -all -inline {X([YZVW])Z} $string] {
    puts "Hey! I found a $matchSubstr in there!"
}

根据口味混合搭配。

我的试用版:[regexp {^X([Y]*)Z$}] 或简单的 regexp {^XYZ$}

只是想确保它是正确的方法。有没有其他方法可以优化正则表达式:)

这对于精确比较来说是最佳的。事实上,如果这是字面意思的话,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$}]

Assuming you're not after literal parentheses around the whole lot, you match that using this:

regexp {X([YZVW])Z} $string -> matchedSubstr

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:

regexp {X(Y|Z|V|W)Z} $string -> matchedSubstr

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 to regexp, typically used in a foreach like this:

foreach {wholeStr matchedSubstr} [regexp -all -inline {X([YZVW])Z} $string] {
    puts "Hey! I found a $matchSubstr in there!"
}

Mix and match to taste.

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 :)

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.

許願樹丅啲祈禱 2024-09-07 00:54:20

我的试用:[re​​gexp {^X([Y|Z|V|W]*)Z$}]

这将匹配给定的字符串,但当您使用 * 乘数时,它也会匹配诸如“XZ”、“XYYYYYYYYYYYYYYYYZ”和“XYZYVWZWWWZVYYWZ”之类的字符串。要仅匹配中间字符一次,请不要使用乘数:

^X([Y|Z|V|W])Z$

我的试用:[re​​gexp {^X([Y]*)Z$}]

同样,它也会匹配“XZ”,“XYYZ”和“XYYYYYYYYYYYYYYYYZ”等字符串。不要在集合后添加乘数:

^X([Y])Z$

或简单的正则表达式 {^XYZ$}

这不会捕获任何内容。要使其与另一个执行相同的操作(捕获 Y 字符),您需要括号:

^X(Y)Z$

My trial: [regexp {^X([Y|Z|V|W]*)Z$}]

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:

^X([Y|Z|V|W])Z$

My trial: [regexp {^X([Y]*)Z$}]

The same there, it will also match strings like "XZ", "XYYZ" and "XYYYYYYYYYYYYYYYYZ". Don't put a multiplier after the set:

^X([Y])Z$

or simply regexp {^XYZ$}

That won't catch anything. To make it do the same as the other (catch the Y character), you need the parentheses:

^X(Y)Z$
若沐 2024-09-07 00:54:20

您可以使用 Visual Regexp 工具来提供帮助,它在您构建正则表达式时提供反馈。

You can use the Visual Regexp tool to help, it provides feedback as you construct your regular expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文