Convert regex matches to the list of strings

I'm trying to find equal sub-string in big list about 50 000 strings, this way fine:

var results = myList.FindAll(delegate (string s) { return s.Contains(myString); });

but it also looks for sub-string with part of word, for example, if I'm looking for "you do" it founds also extra "you dont" because contains "you do..".

So, this answer to my previous question supposedly should work as i need, but I'm not sure, how to get strings list from regex matches for particular code:

foreach (string phrase in matchWordsList)
{
     foreach (string str in bigList)
     {
          string[] stringsToTest = new[] { phrase };
          var escapedStrings = stringsToTest.Select(s => Regex.Escape(s)); 
          var regex = new Regex("\\b(" + string.Join("|", escapedStrings) + ")\\b");
          var matches = regex.Matches(str);
 foreach (string result in matches) /// Incorrect: System.InvalidCastException 
 {
     resultsList.Add(result);
 }

}

Getting strings from matches directly to the list throws exception:

An unhandled exception of type ‘System.InvalidCastException’ occurred in test.exe
Additional information: Unable to cast object of type ‘System.Text.RegularExpressions.Match’ to type ‘System.String’.

So, I’m trying to figure out, hot to convert var matches = regex.Matches(str); to the list

#c-sharp #regex #linq

2 Likes14.50 GEEK