如何做一个{N}?和{n}的有什么不同? [英] How do a{n}? and a{n} differ?

查看:167
本文介绍了如何做一个{N}?和{n}的有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解下面的正前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}?.

这篇关于如何做一个{N}?和{n}的有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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