Regex fast iteration MatchCollection totals

How can I most efficiently use the Index MatchCollection property for each Match

? I have many objects Regex

in my code and I need to iterate over all the indices Match

, but in VS profiler I can see that a simple Linq query

regex.Matches(text).Cast<Match>().Select(x => x.Groups[1].Index)

      

and internal function:

IEnumerator.MoveNext()

      

takes almost half the execution time. Is there a way to do this hardcoded? Maybe the pointer is jumping over internal structures or some other methods to avoid IEnumerable<T>

?

+3


source to share


1 answer


As @LB pointed out, Linq expression is subject to lazy execution . That is, if you go through your MatchCollection at each step, yours Regex

will be executed to ensure the next Match

, and most likely the performance you are seeing.

Regex

Quite heavy actually . But there are some improvements you can make to improve performance (see Regex

best practices
).



What you can try is apply the compiled one Regex

:

Regex comp10 = new Regex(pattern, RegexOptions.Compiled);

      

+2


source







All Articles