1
我有一个数据表包含两列(模式和neworder)和cca 100行(所有具有不同的模式)。循环中的正则表达式类的性能问题
我在做什么是匹配输入字符串与模式(分组匹配),如果匹配发生,我想用Regex.Replace命令重新排列检索到的组。
事情是,正则表达式在循环内部使用时不会表现得非常友好。因为我必须将输入字符串与多个模式匹配,并重新排列输出字符串的外观,所以我完成此任务的唯一方法是使用Regex类。但是这看起来不是一个合适的解决方案,因为它会显着降低性能。
的代码看起来像这样
DataTable dt = this.GetPatterns();
DataRow dr;
System.Collections.IEnumerator ie = dt.Rows.GetEnumerator();
while(ie.MoveNext() && !found)
{
dr = ((DataRow)ie.Current);
pattern = dr["pattern"].ToString();
neworder= dr["neworder"].ToString();
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = reg.Match(input_string);
if (match.Success)
{
found = true;
output = reg.Replace(input_string, neworder);
}
}
不,你的建议是有道理的,但它不适用于我。我添加了Regex.CacheSize = 200,但我最终遇到了同样的问题。我甚至试图创建一个Regex对象数组,所以我不必再次使用同一个对象,但这也没有帮助。 – mko 2010-11-26 10:02:42