有没有办法设定捕获小组的优先级
我想要共享一些字符的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要任何懒惰的操作员 - 只需与
MID
一起明确组Pre
,因此,如果pre> pre> pre>
匹配,则中间
是必需的:(在线演示))
) ,我认为您需要要求
MID
与可选的post
匹配,以便单个t
输入与PRE匹配
仅:(在线演示))
I don't think you need any lazy operators - just explicitly group
pre
together withmid
, so that ifpre
matches thenmid
is required:(online demo)
Or rather, I think you want to require
mid
to match together with an optionalpost
, so that a singleT
input is matched bypre
only:(online demo)