title: How to filter a list of strings matching a pattern
body: <p>I have a list of strings (file names actually) and I'd like to keep only those that match a filter expression like: <strong><code>\*_Test.txt</code></strong>.</p>
  
  <p>What would be the best to achieve this?</p>
  
  <p><strong>Here is the answer that I came up with:</strong></p>
  
  <pre><code>List&lt;string&gt; files = new List&lt;string&gt;();
  files.Add("MyFile1.csv");
  files.Add("MyFile1_Test.txt");
  files.Add("MyFile2.csv");
  files.Add("MyFile2_Test.txt");
  files.Add("MyFile3.csv");
  files.Add("MyFile3_Test.txt");
  files.Add("MyFile_Testtxttxt.txt");
  
  // Define a filter
  string filter = "*_Test.txt";
  
  // Make the filter regex safe
  foreach (char x in @"\+?|{[()^$.#")
      filter = filter.Replace(x.ToString(), @"\" + x.ToString());
  
  filter = string.Format("^{0}$",filter.Replace("*", ".*"));
  
  // Old School
  List&lt;string&gt; resultList1 = files.FindAll(delegate(string s) { return Regex.IsMatch(s, filter, RegexOptions.IgnoreCase); });
  
  // Version using LINQ
  List&lt;string&gt; resultList2 = files.Where(x =&gt;  Regex.IsMatch(x, filter, RegexOptions.IgnoreCase) == true ).ToList();
  </code></pre>