An invalid regex pattern

Here's another one. Anything that ends in a single backslash (dangling backslash) is invalid.

BOOM\

Try this:

*

BTW, in Java there is a method to compile a string to a pattern and it throws an exception with precise error diagnostic.


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...

public string GetRegexError(string _regexPattern, RegexOptions _regexOptions)
{
    try
    {
        Regex _regex = new Regex(_regexPattern, _regexOptions);
    }
    catch (Exception ex)
    {
        return ex.Message;
    }

    return "";
}

Here's an example of a non-correct expression:

[0-9]++