我允许用户输入正则表达式以匹配IP地址,以便在相关系统中进行IP过滤.我想验证输入的正则表达式是否有效,因为很多用户会弄乱op,尽管有良好的意图.
我当然可以在try/catch中做一个Regex.IsMatch(),看看它是否会爆炸,但有没有更智能的方法呢?速度不是一个问题,我只是宁愿避免无缘无故抛出异常.
Jef*_*ood 46
我认为在这种情况下例外是可以的.
这是我放在一起的东西:
private static bool IsValidRegex(string pattern)
{
if (string.IsNullOrEmpty(pattern)) return false;
try
{
Regex.Match("", pattern);
}
catch (ArgumentException)
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*ert 39
只要您捕获非常具体的异常,就可以执行try/catch.
如果使用正确,例外不是邪恶的.
不是没有很多工作。正则表达式解析可能非常复杂,并且框架中没有任何公开内容来验证表达式。
System.Text.RegularExpressions.RegexNode.ScanRegex()
看起来是负责解析表达式的主要函数,但它是内部的(无论如何都会抛出任何无效语法的异常)。因此,您需要重新实现解析功能——这在边缘情况或框架更新时无疑会失败。
我认为在这种情况下捕获 ArgumentException 是一个好主意。
我曾经使用过下面的功能并且没有任何问题。它同时使用异常和超时,但它是实用的。当然,它适用于 .Net Framework >= 4.5。
public static bool IsValidRegexPattern(string pattern, string testText = "", int maxSecondTimeOut = 20)
{
if (string.IsNullOrEmpty(pattern)) return false;
Regex re = new Regex(pattern, RegexOptions.None, new TimeSpan(0, 0, maxSecondTimeOut));
try { re.IsMatch(testText); }
catch{ return false; } //ArgumentException or RegexMatchTimeoutException
return true;
}
Run Code Online (Sandbox Code Playgroud)