In C#, is there any way to try multiple Regexes on string to see which one matches first?
- by Matt
Let's say I have an arbitrary list of regexes (IList<Regex> lst; for example). Is there any way to find out which one matches first?
Of course there is the straightforward solution of trying each one on the string and seeing which match has the lowest index, but this could be inefficient on long strings.
Of course I can go back and pull the strings back out of each regex (Regex.ToString()) and concatenate them all together ("(regex1)|(regex2)|(regex3)"), but I find this to be an ugly solution, especially since it does not even indicate which regex was matched.