模拟所有格量词
是否可以使用原子分组(或其他方式)来模拟所有格量词(.NET 不支持)?
笔记。我发现 (x+x+)++y
可以替换为 (?>(x+x+)+)y
,但这只是一个例子,我不知道 {something}@+
是否总是等于 (?>{something}@)
(其中 @
是量词) 。
Is it possible to emulate possessive quantifiers (.NET doesn’t support it) using atomic grouping (or in other way)?
Note. I found that (x+x+)++y
can be replaced with (?>(x+x+)+)y
, but this is just an example and I don’t know whether always {something}@+
equals to (?>{something}@)
(where @
is a quantifier).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。我可以引用大师本人 Jeffrey Friedl 的经典著作 掌握正则表达式(第三版):
Yup. May I quote the master himself, Jeffrey Friedl, from page 142 of his classic Mastering Regular Expressions (3rd Edition):
不,仅此而已。所有格量词只是原子群的方便简写。
现在,如果您使用的风格也不支持原子组(例如 JavaScript 和 Python),则可以使用前瞻来获得相同的效果:
前瞻的工作方式与原子组类似,只是它不消耗什么它匹配。因此,您将其内容包装在捕获组中,然后使用反向引用来执行消费。
Nope, that's all there is to it. Possessive quantifiers are just a convenient shorthand for atomic groups.
Now, if you were using a flavor that doesn't support atomic groups either (like JavaScript and Python), you could use a lookahead to get the same effect:
A lookahead works just like an atomic group except that it doesn't consume what it matches. So you wrap its contents in a capturing group, then use a backreference to do the consuming.