Search Multiple Strings Using Regular Expression

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.
[surcecode language=”csharp”]
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.

Author:Jebarson | Follow him


Jebarson is a consultant at Microsoft. In his overall experience of 7+ years, his expertise ranges from VB6, COM / DCOM, .net,, WPF, WCF, SL, SQL. He has a greater love for OOA / OOD and SOA. His current focus is on Azure, Windows Phone 7, Crm and much more. He is also a frequent speaker of different community events. He blogs at . You can follow him at @Jebarson007 . Jebarson having good set of tutorials written on Windows Azure, you can found them . He is a contributor of this site and shared many tips and tricks.

One Comment to “Search Multiple Strings Using Regular Expression”

Comments are closed.