views:

34

answers:

1

Given the following code:

var myList = new List<string> { "red", "blue", "green" };
Regex r = new Regex("\\b(" + string.Join("|", myList.ToArray()) + ")\\b");
MatchCollection m = r.Matches("Alfred has a red and blue tie and blue pants.");

Is there a way to derive a List<string> of the "found" items ("red", "blue", "blue")?

+2  A: 
var n = (from Match match in m
         select match.Value).ToList()
erash
minor typo fixed.
Mitch Wheat
Wow. I was using foreach (var match in m). When I changed it to foreach (Match match in m), it works. Thanks!
Brian David Berman
Since m does not impelement IEnumerable<T> you need to specifically tell the compiler that you are dealing with Match objects. Presumably your var was an object
erash