非贪婪的正则表达式量词给贪婪的结果 [英] Non-greedy regex quantifier gives greedy result

查看:248
本文介绍了非贪婪的正则表达式量词给贪婪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET正则表达式,我使用Windows PowerShell正在测试。所述输出如下:

I have a .net regex which I am testing using Windows Powershell. The output is as follows:

> [System.Text.RegularExpressions.Regex]::Match("aaa aaa bbb", "aaa.*?bbb")


Groups   : {aaa aaa bbb}
Success  : True
Captures : {aaa aaa bbb}
Index    : 0
Length   : 11
Value    : aaa aaa bbb

我的期望是,使用量词会导致比赛以 AAA BBB ,作为第二组一的的是足以满足前pression。是我的非贪婪量词的认识存在缺陷,还是我测试的错误?

My expectation was that using the ? quantifier would cause the match to be aaa bbb, as the second group of a's is sufficient to satisfy the expression. Is my understanding of non-greedy quantifiers flawed, or am I testing incorrectly?

请注意:这显然是不一样的问题,普通防爆pression nongreedy是贪婪

Note: this is plainly not the same problem as Regular Expression nongreedy is greedy

推荐答案

这是一个普遍的误解。懒惰量词不保证最短的比赛。它们仅确保当前量词,从当前位置,不超过所需的总体匹配匹配多个字符。

This is a common misunderstanding. Lazy quantifiers do not guarantee the shortest possible match. They only make sure that the current quantifier, from the current position, does not match more characters than needed for an overall match.

如果你真的想确保在最短的比赛,你需要做出明确的。在这种情况下,这意味着*?,你想有一个subregex匹配任何既不是 AAA 而不是。也不 BBB 。因此,由此产生的正则表达式将是

If you truly want to ensure the shortest possible match, you need to make that explicit. In this case, this means that instead of .*?, you want a subregex that matches anything that is neither aaa nor bbb. The resulting regex will therefore be

aaa(?:(?!aaa|bbb).)*bbb

这篇关于非贪婪的正则表达式量词给贪婪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆