如何在TCL中使用proc编写正则表达式来处理以下模式?

发布于 2024-08-30 18:32:05 字数 213 浏览 3 评论 0原文

我是 TCL 新手,寻求帮助来处理以下表达式。 我从用户那里获取 i/p 字符串来验证下面的任何字符串 & CLI 使用过程中的一行中没有其他字符

{ GHI GII GJI GKI}

,另一个棘手的问题是编写正则表达式以仅匹配以字母 A & 开头的字符。以 B 结尾,在使用过程之间还有 1 个或多个 YO 或 OY。 谢谢

I am new to TCL and seeking a help to deal with the following expression.
I am getting the i/p string from the user to validate any of these strings below & no others in a line in CLI using procedure

{ GHI GII GJI GKI}

and another tricky one is to write regexp to match only the characters which begin with alphabet A & end with B, It also have 1 or more of either YO or OY in between using procedure.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ヤ经典坏疍 2024-09-06 18:32:05

如果这是您的输入,那么实际上不需要使用正则表达式:只需检查提供的单词是否在该列表中:

set input { GHI GII GJI GKI}
foreach word {GJI GLI} {
    if {$word in $input} {
        puts "$word is in [list $input]"
    } else {
        puts "$word is not in [list $input]"
    }
}

匹配“以字母 A 开头并以 B 结尾,它还有 1 个或多个 YO或 OY 之间”:

set re {^A(?:YO|OY)+B$}
foreach word {AYOB AYOOYB AYYB} {
    if {[regexp $re $word]} {
        puts "$word matches"
    } else {
        puts "$word does not match"
    }
}

如果您的意思是“(1 个或多个 YO)或(1 个或多个 OY),则正则表达式为

set re {^A(?:(?:YO)+|(?:OY)+)B$}

If that's your input, then really there's no need to use regular expressions: just check that a supplied word is in that list:

set input { GHI GII GJI GKI}
foreach word {GJI GLI} {
    if {$word in $input} {
        puts "$word is in [list $input]"
    } else {
        puts "$word is not in [list $input]"
    }
}

A regex that matches "begin with alphabet A & end with B, It also have 1 or more of either YO or OY in between":

set re {^A(?:YO|OY)+B$}
foreach word {AYOB AYOOYB AYYB} {
    if {[regexp $re $word]} {
        puts "$word matches"
    } else {
        puts "$word does not match"
    }
}

If you mean "either (1 or more of YO) or (1 or more of OY), then the regex is

set re {^A(?:(?:YO)+|(?:OY)+)B$}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文