Search Multiple Strings Using Regular Expression

By | February 24, 2011

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);
[/sourcecode]

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

Category: Tips Tags: , , , , ,

About The Author

About Jebarson

Jebarson is a consultant at Microsoft. In his overall experience of 7+ years, his expertise ranges from VB6, COM / DCOM, .net, ASP.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 http://www.jebarson.info/ . You can follow him at @Jebarson007 . Jebarson having good set of tutorials written on Windows Azure, you can found them http://bit.ly/houBNx . He is a contributor of this site and shared many tips and tricks.

One thought on “Search Multiple Strings Using Regular Expression

  1. Pingback: Tweets that mention Search Multiple Strings Using Regular Expression #tips #net #c #match #regex -- Topsy.com

Comments are closed.