【发布时间】:2011-05-15 23:21:04
【问题描述】:
我有一个数据表,其中包含两列(pattern 和 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);
}
}
【问题讨论】:
标签: .net regex performance memory-leaks