Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An invalid regex pattern

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?

like image 356
Marcom Avatar asked Aug 17 '11 15:08

Marcom


People also ask

What does invalid regular expression mean?

The JavaScript exception "invalid regular expression flag" occurs when the flags in a regular expression contain any flag that is not one of: g , i , m , s , u , y or d . It may also be raised if the expression contains more than one instance of a valid flag.

How do I match a pattern in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .


1 Answers

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 ""; } 
like image 124
Steve Wortham Avatar answered Sep 25 '22 06:09

Steve Wortham