What is a "Nested Quantifier" and why is it causing my regex to fail?

.NET is complaining about the + after the {n} style quantifier as it doesn't make any sense. {n} means match exactly n of a given group. + means match one or more of a given group. Remove the +'s and it'll compile fine.

"_ [ 0-9]{10} {1}[ 0-9]{10} {2}[ 0-9]{6} {2}[ 0-9]{2}"

.NET doesn't support the possessive quantifier

{10}+

However, {10} should have exactly the same effect. The + avoids backtracking and trying shorter matches if the longest match fails, but since {10} can only match exactly 10 characters to start with this doesn't achieve much.

"_ [ 0-9]{10} [ 0-9]{10} {2}[ 0-9]{6} {2}[ 0-9]{2}"

should be fine. I've also dropped the "{1}+" bit .Since it matches exactly once, "A{1}+" is equivalent to just "A".

EDIT As Porges says, if you do need possessive quantifiers in .NET, then atomic groups give the same functionality with (?>[0-9]*) being equivalent to [0-9]*+.


They're right. This version of your regex doesn't fail:

(_ [ 0-9]{10})+(\s{1})+([ 0-9]{10})+(\s{2})+([ 0-9]{6})+\s{2}[ 0-9]{2}

Notice the use of parens to create groups that then can repeat one or more times. Also, you should be more specific and use \s instead of a space, as pattern whitespace may or may not have significance.

BTW, this regex doesn't look all that useful. You might want to ask another question along the lines of "How do I use regex to match this pattern?"

Tags:

C#

.Net

Regex