3

I wanted to know if it was possible to use a Regex rule in a linq request. Indeed I would like to know if an ID contains the word "Download".

Test 1:

foreach (var apiData in viewReturn.Where(x => x.ID.Contains("Download")))
  {
    apiExterne.Add(apiData);
  }

This format works

Test 2:

foreach (var apiData in viewReturn.Where(x => Regex.IsMatch(x.ID, "^[dD][oO][wW][nN][lL][oO][aA][dD]$")))
  {
    apiExterne.Add(apiData);
  }

This one does not work.

Thank you in advance for your answers.

Space
  • 124
  • 1
  • 13
  • And what does "does not work" mean? Does it not find a match? Do you get an error? – JuanR Aug 03 '17 at 14:50
  • Hello @Space and welcome to SO ! What do you mean by "This one does not work". Could you please elaborate your exact question, and detail the error you encounter ? Also you may be interested in the [how to ask documentation](https://stackoverflow.com/help/how-to-ask). – John-Philip Aug 03 '17 at 14:51
  • Have you tried simplifying - Where(x => x != nulll && Regex.IsMatch(x.ID.ToUpper(), "DOWNLOAD"))) - and remove the anchors if you only want to check if it contains the word download – David Lindon Aug 03 '17 at 14:52

3 Answers3

3

Adding ^ and $ characters to the start and end of the regex means that the entire string should match, not just a substring. So technically your regex is not equivalent to Contains() function, which would return true for substring match too.

Remove those two characters from your substring. Also you do not need both upper and lower case letters, instead you can use IgnoreCase option.

You should not use regex for such a simple scenarios. If the only reason you're using Regex is that your string could use any combination of upper and lower cases, check this excellent post for a Contains() function that can ignore case for you. Alternately, you could also call ToLower() in your LINQ:

foreach (var apiData in viewReturn.Where(x => x.ID.ToLower().Contains("download")))
dotNET
  • 33,414
  • 24
  • 162
  • 251
2

You used anchors, ^ (start of string) and $ (end of string), that require a full string match. Also, there is no need using character classes listing all letter cases, use the case insensitive regex flag.

If you want to use a regex use

foreach (var apiData in viewReturn.Where(x => 
          Regex.IsMatch(x.ID, "download", RegexOptions.IgnoreCase)))
{
   apiExterne.Add(apiData);
}

A non regex solution is considered the best approach:

foreach (var apiData in viewReturn.Where(x => 
          culture.CompareInfo.IndexOf(x, "download", CompareOptions.IgnoreCase) >= 0))
{
   apiExterne.Add(apiData);
}

See this SO thread for details.

Graham
  • 7,431
  • 18
  • 59
  • 84
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-1

As explained in the microsoft docs, this is what you should do:

System.Text.RegularExpressions.Regex searchTerm =  
        new System.Text.RegularExpressions.Regex(@"^[dD][oO][wW][nN][lL][oO][aA][dD]$");  
var queryMatching =  
        from item in viewReturn  
        let itemID = item.ID  
        let matches = searchTerm.Matches(itemID)  
        where matches.Count > 0  
        select new  
        {  
            id = item.ID,  
            apiData = from System.Text.RegularExpressions.Match match in matches  
                            select match.Value  
        };  
leandrosa81
  • 138
  • 8