无效的正则表达式 [英] An invalid regex pattern

查看:647
本文介绍了无效的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在来检查,如果值是一个有效的正则表达式C#中的一段代码。
码是直截了当:

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)
   {
       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.. hehe

有什么建议?

推荐答案

这是无效...

[

您还可以测试在实时的正则表达式的有效性< A HREF =http://regexhero.net/tester/> http://regexhero.net/tester/

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

这篇关于无效的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆