Reputation: 414
int characterLimit = 5;
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}+$");
if(!string.IsNullOrEmpty(e.NewTextValue))
if (!Regex.IsMatch( e.NewTextValue, regxForAlpha)){
}
else
{
}
This code is throwing NestedQuantifier exception. Can any one know why?
Upvotes: 1
Views: 616
Reputation: 627469
Here is a fixed code:
string NewTextValue = "str";
int characterLimit = 5;
string regxForAlpha = "^[a-zA-Z \n]{0,"+characterLimit.ToString()+"}$";
if(!string.IsNullOrEmpty(NewTextValue))
if (!Regex.IsMatch( NewTextValue, regxForAlpha)){
Console.WriteLine("No match");
}
else
{
Console.WriteLine("Match");
}
See IDEONE demo (changed e.NewTextValue
to NewTextValue
for demo purposes).
There are several points of interest:
Regex.IsMatch
accepts a string, not a Regex object, as its second parameter{0,5}+
- and that +
caused the nested quantifier issue).[a-zA-Z \n] {0,5}
, the {0,5}
is applied to the space that stands next to it on the left, and the meaning of the regex is somewhat distorted.Upvotes: 1
Reputation: 390
Please, change
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}+$");
to
Regex regxForAlpha = new Regex("^[a-zA-Z \n] {0,"+characterLimit.ToString()+"}$");
Upvotes: 0