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
Pingback: Tweets that mention Search Multiple Strings Using Regular Expression #tips #net #c #match #regex -- Topsy.com