Brian David Berman
Brian David Berman

Reputation: 7684

Get found items as list from regex

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")?

Upvotes: 0

Views: 1269

Answers (1)

cordialgerm
cordialgerm

Reputation: 8503

var n = (from Match match in m
         select match.Value).ToList()

Upvotes: 2

Related Questions