How to handle the parsing of nested quantifier?
I have my code as
Regex regExValue = new Regex(cirtText.Properties.Mask.EditMask);
but i get an exception as
Message : parsing "((www).([a-zA-Z0-9]{1,6}+.+)*[a-zA-Z]{2,6})" - Nested quantifi开发者_运维知识库er +.
can anyone let me know the solution?
You have two quantifiers {1,6}
(between 1 and 6 only) and +
(at least 1) here. That's your error. You need to choose one.
[a-zA-Z0-9]{1,6}+
Anyway, you probably meant to write your regex like this:
((www)\.([a-zA-Z0-9]{1,6}\.)*[a-zA-Z]{2,6})
I don't know C# regex or what you are trying to do (ie: edit, validate,.. etc)
I know Perl, so I'll take a stab at it.
In regex's a period is a meta character. If you want a literal period, you have to escape
it with a '.' The meta period says match any character. If you want a literal period,
and to keep what you have, in Perl you either should not double quote the regex or escape
the escape. A single quote should work fine.
'((www)\.([a-zA-Z0-9]{1,6}+\.+)*[a-zA-Z]{2,6})'
Now, unless you want multiple literal periods, you should get rid of the + quantifier
'((www)\.([a-zA-Z0-9]{1,6}+\.)*[a-zA-Z]{2,6})'
As was said, {n,m} itself is a quantifier. Adding + after it is a double quantifier.
In Perl adding + after a quantifyer implies a possesive condition and is legal starting in version 5.10
So, getting rid of the extra + it is now
'((www)\.([a-zA-Z0-9]{1,6}\.)*[a-zA-Z]{2,6})'
Finally, the * quantifyer implies 0 or more times. Why would you want to match 0 times?
Fixing that it is now
'((www)\.([a-zA-Z0-9]{1,6}\.)+[a-zA-Z]{2,6})'
As an extra, you have a main grouping around everything, a grouping around www, and one
around the middle ([a-zA-Z0-9]{1,6}.)+ which does no good in capture, just in grouping.
If you want to capture the beginning, middle, end, you should add appropriate capture
'((www\.)(([a-zA-Z0-9]{1,6}\.)+)([a-zA-Z]{2,6}))'
Or, in Perl, that would be better written as
'((www\.)((?:[a-zA-Z0-9]{1,6}\.)+)([a-zA-Z]{2,6}))'
As others have mentioned, having two consecutive quantifiers is meaningless. It looks like you may have meant to say "one or more of these groups of one to six characters"; is it possible that you've left out parenthesis? If that's what you meant, this is easy to resolve:
(note that in .NET regexes, if you actually want a dot you have to escape the period, otherwise it'll match anything)
@"((www)\.(([a-zA-Z0-9]{1,6}+)\.+)*[a-zA-Z]{2,6})"
You could visit here link text
to find out how to use a regular expression as a Mask. To be honest, your question isin't about regular expressions. There is some peculariarity about your presentation code that suggests DevExpress implementation of RegEx and Masks. You might want to check there. ie:
DevExpress
For more information on regular expressions, see the "Mask Type: Full Functional Regular Expressions" document in the help documentation of the XtraEditors Library.
You look like you are using DevExpress with .Net's Regex class. But DevExpress has thier own implementation it seems.
Also, the repetitive input via quantifier might be questionable in relation to a mask. Regardless, you could try this but its just a guess:
'www\.(([a-zA-Z0-9]{1,6}\.?)+)*\.[a-zA-Z]{2,6}'
精彩评论