I have a piece of code in c# that checks, if a value is a valid regex pattern.
Code is straight forward:
try
{
System.Text.RegularExpressions.Regex.IsMatch("", pattern);
}
catch (Exception ex)
{
return "pattern matches must be a valid regex value";
}
I’m trying to test if it works correctly, but I can’t find an invalid regex pattern.
Any suggestions?
This is invalid…
You can also test the validity of regular expressions in real-time at http://regexhero.net/tester/
By the way, you don’t actually have to test the regular expression against a string to see if it’s valid. You can simply instantiate a new Regex object and catch the exception.
This is what Regex Hero does to return a detailed error message…