如何验证正防爆pression?pression

2023-09-03 12:07:24 作者:人言可畏

我正在开发的.NET应用程序,用户可以提供事后被用来验证输入数据的普通防爆pressions。

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

我需要一种方法来知道,如果一个普通的前pression实际上是适用于.NET正则表达式引擎。

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

感谢您的帮助

推荐答案

只是试图编译给定的正则表达式。你可以通过创建正则表达式对象,并传递模式给它。下面是一个示例code:

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;
}