Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

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

I have this regex I built and tested in regex buddy.

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

When I use this in .Net C#

I receive the exception

"parsing "_ [ 0-9]{10}+ +[ 0-9]{10}+  +[ 0-9]{6}+  [ 0-9]{2}" - Nested quantifier +."

What does this error mean? Apparently .net doesn't like the expression.

Here is the regex buddy so u can understand my intention with the regex...

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

Match the characters "_ " literally ?_ ?
Match a single character present in the list below ?[ 0-9]{10}+?
   Exactly 10 times ?{10}+?
   The character " " ? ?
   A character in the range between "0" and "9" ?0-9?
Match the character " " literally ? {1}+?
   Exactly 1 times ?{1}+?
Match a single character present in the list below ?[ 0-9]{10}+?
   Exactly 10 times ?{10}+?
   The character " " ? ?
   A character in the range between "0" and "9" ?0-9?
Match the character " " literally ? {2}+?
   Exactly 2 times ?{2}+?
Match a single character present in the list below ?[ 0-9]{6}+?
   Exactly 6 times ?{6}+?
   The character " " ? ?
   A character in the range between "0" and "9" ?0-9?
Match the character " " literally ? {2}?
   Exactly 2 times ?{2}?
Match a single character present in the list below ?[ 0-9]{2}?
   Exactly 2 times ?{2}?
   The character " " ? ?
   A character in the range between "0" and "9" ?0-9?

In short...

What is a Nested quantifier?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

.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}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...