有没有办法设定捕获小组的优先级

发布于 2025-01-30 04:50:24 字数 735 浏览 2 评论 0原文

我想要共享一些字符的3个捕获组(前/中/职位)。这三个都可以匹配0或1次,类似的是:

^(?<pre>[T])?(?<mid>[T][A-Z])?(?<post>[T])?$

我正在寻找按顺序匹配的:MID-&GT; pre-&gt;发布。 Regex我发布了3/4个字符串的作品,例如ttt / ttu / tut < / code>。对于两个字符字符串tt,我想匹配组中间 - 结果是pre:p pre:t,post:t。我知道为什么会发生这种情况 - pre匹配,中间找不到两个要匹配的字符,post匹配。

为了MID要优先考虑,我尝试了组和懒惰操作员的组合,但是大多数在pree> pre> pre> pre之前导致post匹配,例如此示例:

^(?<pre>[T])??(?<mid>[T][A-Z])?(?<post>[T])?$

有什么方法可以允许MID组在pre> pre之前匹配?您可以设置某种匹配顺序吗?

I want 3 capturing groups (pre/mid/post) that share some characters. All three can match 0 or 1 time, something like this:

^(?<pre>[T])?(?<mid>[T][A-Z])?(?<post>[T])?$

I am looking for them to match in order: mid -> pre -> post. Regex I've posted works for 3/4 letter strings, like TTT / TTU / TUT. For two character string TT, I would like for group mid to be matched - the result is pre: T, post: T though. I understand why that's happening - pre matches, mid doesn't find two character to match, post matches.

For mid to take precedence I've tried combinations of groups and lazy operators, but most of them resulted in post matching before pre, like for this example:

^(?<pre>[T])??(?<mid>[T][A-Z])?(?<post>[T])?$

Is there any way to allow mid group to match before pre? Can you set some kind of order of matching?

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

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

发布评论

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

评论(1

箹锭⒈辈孓 2025-02-06 04:50:24

我认为您不需要任何懒惰的操作员 - 只需与MID一起明确组Pre,因此,如果pre> pre> pre>匹配,则中间是必需的:

^(?:(?<pre>T)?(?<mid>T[A-Z]))?(?<post>T)?$

在线演示

) ,我认为您需要要求MID与可选的post匹配,以便单个t输入与PRE匹配仅:

^(?<pre>T)?(?:(?<mid>T[A-Z])(?<post>T)?)?$

在线演示

I don't think you need any lazy operators - just explicitly group pre together with mid, so that if pre matches then mid is required:

^(?:(?<pre>T)?(?<mid>T[A-Z]))?(?<post>T)?$

(online demo)

Or rather, I think you want to require mid to match together with an optional post, so that a single T input is matched by pre only:

^(?<pre>T)?(?:(?<mid>T[A-Z])(?<post>T)?)?$

(online demo)

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