C# 是否可以同时检查多个正则表达式匹配?一次迭代?
好的,我的问题是关于检查性能 假设我有以下正则表达式C# 是否可以同时检查多个正则表达式匹配?一次迭代?,c#,.net,regex,.net-4.5.2,C#,.net,Regex,.net 4.5.2,好的,我的问题是关于检查性能 假设我有以下正则表达式 regex1 = "=powered\ by\ 4images" regex2 = "post\ comment" regex3 = "kommentar\ posten" regex4 = "\[url=.*?.*?.*?.*?://" string mystring=""; 现在我想要的是这个 if( Regex.IsMatch(srinput, regex1 , RegexOption
regex1 = "=powered\ by\ 4images"
regex2 = "post\ comment"
regex3 = "kommentar\ posten"
regex4 = "\[url=.*?.*?.*?.*?://"
string mystring="";
现在我想要的是这个
if( Regex.IsMatch(srinput, regex1 , RegexOptions.IgnoreCase)
&& Regex.IsMatch(srinput, regex2 , RegexOptions.IgnoreCase)
&& (Regex.IsMatch(srinput, regex3 , RegexOptions.IgnoreCase)
|| Regex.IsMatch(srinput, regex4 , RegexOptions.IgnoreCase)))
这将使每个选项都成为字符串文本的完整扫描查询,对吗
不管怎样,这件事可以加快吗?我不知道在一次迭代中检查所有正则表达式等
这是实现多个regex IsMatch检查的最有效的方法吗
c.net 4.5.2 wpf应用程序静态reglex.IsMatch内部调用返回新的regexpatern、options、matchTimeout、true.IsMatchinput; 如果您执行大量检查,并且可能创建一个实例并重用它,那么它将节省一些性能。代码如下:
string mystring = "MonsterMMORPG";
var sw = new Stopwatch();
sw.Start();
int count = 10000;
var regex1 = @"=powered\ by\ 4images";
for (int i = 0; i < count; i++)
{
if (Regex.IsMatch(mystring, regex1, RegexOptions.IgnoreCase))
{
}
}
sw.Stop();
Console.WriteLine(string.Format("using Static Check:{0}", sw.Elapsed));
sw = new Stopwatch();
var r = new Regex(regex1,RegexOptions.IgnoreCase);
sw.Start();
for (int i = 0; i < count; i++)
{
if (r.IsMatch(mystring))
{
};
}
sw.Stop();
Console.WriteLine(string.Format("using instance Check:{0}", sw.Elapsed));
输出为:
使用静态检查:00:00:00.0074411
使用实例检查:00:00:00.0006221
无论哪种方式,regrex检查都非常快,但实例一的速度稍快静态regrex.IsMatch内部调用返回新的REGREXPATTERN、options、matchTimeout、true.IsMatchinput; 如果您执行大量检查,并且可能创建一个实例并重用它,那么它将节省一些性能。代码如下:
string mystring = "MonsterMMORPG";
var sw = new Stopwatch();
sw.Start();
int count = 10000;
var regex1 = @"=powered\ by\ 4images";
for (int i = 0; i < count; i++)
{
if (Regex.IsMatch(mystring, regex1, RegexOptions.IgnoreCase))
{
}
}
sw.Stop();
Console.WriteLine(string.Format("using Static Check:{0}", sw.Elapsed));
sw = new Stopwatch();
var r = new Regex(regex1,RegexOptions.IgnoreCase);
sw.Start();
for (int i = 0; i < count; i++)
{
if (r.IsMatch(mystring))
{
};
}
sw.Stop();
Console.WriteLine(string.Format("using instance Check:{0}", sw.Elapsed));
输出为:
使用静态检查:00:00:00.0074411
使用实例检查:00:00:00.0006221
无论哪种方式,regrex检查都非常快,但实例检查稍微快一些只要在regrex的每个条件之间添加一个|,然后立即匹配即可
(=powered\ by\ 4images)|(post\ comment)|(kommentar\ posten)|(\[url=.*?.*?.*?.*?:)
只需在正则表达式的每个条件之间添加一个|,然后立即匹配即可
(=powered\ by\ 4images)|(post\ comment)|(kommentar\ posten)|(\[url=.*?.*?.*?.*?:)
可以使用模拟布尔AND&&和模拟正则表达式中的布尔OR
然后,您不需要转义正则表达式中的空格字符,但如果希望它与文字点匹配,则应该转义该点。我猜你的意思是这样的:
Regex regex1 = new Regex(@"
^ # Either make sure that at the start of the string
(?=.*=powered by 4images) # it's possible to match this subregex
(?=.*post comment) # and this one
(?=.*kommentar posten) # and this one
| # or
\[url=.*?\..*?\..*?\..*?:// # match this regex anywhere in the string",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
可以使用模拟布尔AND&&和模拟正则表达式中的布尔OR
然后,您不需要转义正则表达式中的空格字符,但如果希望它与文字点匹配,则应该转义该点。我猜你的意思是这样的:
Regex regex1 = new Regex(@"
^ # Either make sure that at the start of the string
(?=.*=powered by 4images) # it's possible to match this subregex
(?=.*post comment) # and this one
(?=.*kommentar posten) # and this one
| # or
\[url=.*?\..*?\..*?\..*?:// # match this regex anywhere in the string",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
但是|是替代或运算符-OP需要检查和条件以及查看其代码中的&&s…但是|是替代或运算符-OP需要检查和条件以及查看其代码中的&&s……并且您可能希望使用\[url=[^.]*.[^.]*...*.*.*.*.*.*.*.*.*.*.*.[^:]*://而不是正则表达式最后部分中的那些不特定的。*?s….并且您可能希望使用\[url=[^.]*\.[^.]*.*.[^.]*.[^:::://而不是正则表达式最后部分中的那些不特定的。*?s。