This is small piece of code snippet which I wanted to share as we face this necessity in our day to day programming. Suppose you have a need when you need to search multiple string, say you need to search strings like “int”, “float”, “string” in a piece of text. Below is how you will achieve it without using any loop and in a simple and straight forward way.

string pattern = "int|float|string";
MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);

Since the return value is a “MatchCollection”, you can find the index of the matched string and many other properties / methods supported by “Match” type.


Search Multiple Strings Using Regular Expression