Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate a Regular Expression? [duplicate]

Tags:

c#

.net

regex

I'm developing an application in .NET where the user can provide Regular Expressions that are afterwards used to validate input data.

I need a way to know if a regular expression is actually valid for the .net regex engine.

Thanks for any help

like image 945
Carlos G. Avatar asked Sep 03 '09 04:09

Carlos G.


1 Answers

Just try to compile the given regex. You can do that by creating the Regex object and passing the pattern to it. Here's a sample code:

public static bool IsRegexPatternValid(String pattern)
{
    try
    {
        new Regex(pattern);
        return true;
    }
    catch { }
    return false;
}
like image 58
Paulius Avatar answered Oct 09 '22 16:10

Paulius