In C#, is there any way to try multiple Regexes on string to see which one matches first?
Posted
by
Matt
on Stack Overflow
See other posts from Stack Overflow
or by Matt
Published on 2012-11-29T11:02:02Z
Indexed on
2012/11/29
11:03 UTC
Read the original article
Hit count: 274
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.
© Stack Overflow or respective owner