An invalid regex pattern

后端 未结 5 2054
灰色年华
灰色年华 2021-02-07 01:52

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


        
5条回答
  •  既然无缘
    2021-02-07 02:25

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

提交回复
热议问题