Regex IsMatch taking too long to execute
I have one strange issue on my .NET project with RegEx. Please, see C#
code below:
const string PATTERN = @"^[a-zA-Z]([-\s\.a-zA-Z]*('(?!'))?[-\s\.a-zA-Z]*)*$";
const string VALUE = "Ingebrigtsen Myre (Øvre)";
System.Text.RegularExpressions.Regex regex = new
System.Text.RegularExpressions.Regex(PATTERN);
if (!regex.IsMatch(VALUE)) // <--- Infinite loop here
return string.Empty;
// Some other code
I use this pattern to validate all types of names (fist names, last names,
middle names, etc.). Value is a parameter, but I provided it as a constant
above, because issue is not reproduced often - only with special symbols:
*, (, ), etc. (sorry, but I don't have the full list of these symbols).
Can you help me to fix this infinite loop? Thanks for any help.
Added: this code is placed on the very base level of project and I don't
want to do any refactoring there - I just want to have quick fix for this
issue.
Added 2: I do know that it technically is not a loop - I meant that
"regex.IsMatch(VALUE)" never ends. I waited for about an hour and it was
still executing.
No comments:
Post a Comment