Loading ...

RegEx: Quantifier {x,y} following nothing error | CodeAsp.Net

RegEx: Quantifier {x,y} following nothing error

 /5
0 (0votes)

While parsing one of my regex pattern based on input data I was getting the following error:

RegEx: Quantifier {x,y} following nothing error

I was having the following code:

string pattern = string.Format(@"\b({0})\b", string.Join("|", dic.Select(p => p.Key).ToArray()));
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);

Dictionary reffered above was from input data from user. I noticed the issue was coming because of the reserved characters of regex which user entered in his input data. The solution was to strip those characters. I fixed it in the following way:

string pattern = string.Format(@"\b({0})\b", string.Join("|", dic.Select(p => Regex.Replace(p.Key, "[~#%&*{}/<>?|\"-+().]", "")).ToArray()));


i.e I added the following code:

Regex.Replace(p.Key, "[~#%&*{}/<>?|\"-+().]", "")



Comments (no comments yet)

Top Posts