Saturday, 24 August 2013

Why is For loop on Regex Matches slow?

Why is For loop on Regex Matches slow?

I have the following code:
string pattern = @"(?:\S+\s){1,6}\S*" + search +
@"\S*(?:\s\S+){1,6}";
String dbContents = row[2].ToString();
var matches = Regex.Matches(dbContents, pattern,
RegexOptions.IgnoreCase | RegexOptions.Compiled);
for (int i = 0; i < matches.Count; i++)
{
if (i == 3)
break;
Contents += String.Format("... {0} ...", matches[i].Value);
}
What I'm trying to accomplish is to get one to six words before the search
term and 1-6 words after the search term. When executing the code the
performance hit on the for loop "matches.Count". With very large strings,
its taking upwards of a min to execute. I'm confused on why and what to do
to fix the issue.

No comments:

Post a Comment