I have the following code to extract specific tokens from an input file
string sLine = File.ReadAllText(ituffFile);
Regex rxp = new Regex(@"2_tname_(?<token>\S+)", RegexOptions.Compiled);
MatchCollection rxpMatches = rxp.Matches(sLine);
Now i want to convert the MatchCollection, which holds the elements, i’m looking for to a HashSet.
What is the fastest way to achieve this?
Is the following the best way?
HashSet<string> vTnames = new HashSet<string>();
foreach (Match mtch in rxpMatches)
{
vTnames.Add(mtch.Groups["token"].Value);
}
Yeah according to me your code is perfect because there is not any suitable casting seems for MatchCollection to HastSet.So the way you are following using foreach loop is perfect..