Friday, 17 December 2010

Loading Regex matches to a varaible using Linq

How to get your Regex matches into a Linq variable:

string inString = "ABCDEFGH"
Regex re = new Regex(@"\w{1}");
var list = (from Match m in re.Matches(inString) select m.Value).ToList()


Another easy way to make sure you get words and numbers:


var list = inString.Select(x => 
                {
                    return Regex.Match(x.ToString(), "[a-zA-Z0-9]+");
                });

No comments:

Post a Comment