我想了解下面的正前pression量词(的在的仅仅是一个示范这里令牌):
A {N}?
如何问号影响上述前pression比赛?它是如何从下面的有什么不同?
A {N}
我本来期望的模式 AA {1}?一个
同时匹配 AAA
和 AA
为例。虽然它匹配 AAA
, AA
是不可以匹配。该模式 A({1})?A
并同时匹配,所以括号确有差别在这里。
注:普通防爆pressions 状态MSDN文章量词为:
在{n}的量词的preceding元素完全匹配n次, 其中n是任意整数。
有关 {N}
,它增加了以下内容,不是太有帮助的部分:
这是贪婪的量词{n}的懒惰对应+。
解决方案没有。文章说:
在 {N} 量词的preceding元素完全匹配的 N 的时间,其中的 N 的任何整数。 { N 的} 是一个贪婪的量词的懒惰当量的 { N 的}?
...
在 {n}的?量词的preceding元素完全的 N 的时间,其中的 N 的任何整数匹配。这是贪婪的量词的懒惰对应的 { N 的} +
注意文本是完全一样的。基本上,增加 不改变量词的行为。看来,.NET的正前pression引擎支持 {n}的?
作为替代 {N}
有趣的是,这篇文章确实出现了包含一个错误:
在 { N 的,} 量词的preceding元素至少匹配的 N 的时间,其中的 N 是任何整数。 { N,的} 是一个贪婪的量词的懒惰当量的 { N 的}?
这是错误的。懒惰相当于 {N,}
是 {N,}?
是没有的同 {n}的?
。
I'm trying to understand the following regular expression quantifier (a is just an exemplary token here):
a{n}?
How does the question mark affect the match of the above expression? And how does it differ from the following?
a{n}
I would have expected the pattern aa{1}?a
to match both aaa
and aa
for example. While it matches aaa
, aa
is not a match. The pattern a(a{1})?a
does match both, so the parentheses do make a difference here.
Note: The msdn article Quantifiers in Regular Expressions states for both:
The {n} quantifier matches the preceding element exactly n times, where n is any integer.
For {n}?
, it adds the following, not overly helpful part:
It is the lazy counterpart of the greedy quantifier {n}+.
解决方案
Nothing. The article states:
The {n} quantifier matches the preceding element exactly n times, where n is any integer. {n} is a greedy quantifier whose lazy equivalent is {n}?.
…
The {n}? quantifier matches the preceding element exactly n times, where n is any integer. It is the lazy counterpart of the greedy quantifier {n}+.
Notice the text is exactly the same. Basically, adding ? does not change the behavior of the quantifier. It appears that .NET's regular expression engine supports {n}?
as a alternative to {n}
.
Interestingly, this article does appear to contain an error:
The {n,} quantifier matches the preceding element at least n times, where n is any integer. {n,} is a greedy quantifier whose lazy equivalent is {n}?.
This is wrong. The lazy equivalent of {n,}
is {n,}?
which is not the same as {n}?
.