1

I've got a system in C# where by a set of keywords are required. Two fields have to be checked if the keywords exist.

Initially I wrote a foreach loop to go through each keyword, then cycle through the results and check. However, this is somewhat inefficient, as on check, I would like to see if any of the keywords exist in a given string, rather than one by one.

Thanks.

ElHaix
  • 12,846
  • 27
  • 115
  • 203

3 Answers3

2

Does this question match what you are trying to do?

The answer shows you how to match multiple possible words in one go with a regex

Community
  • 1
  • 1
Russell Troywest
  • 8,635
  • 3
  • 35
  • 40
1

If you are on c# 3.5+ try this

        Regex r1 = new Regex("MyKeywordRegex");
        IEnumerable<MyResultClass> results = GetMyResults();
        var myFilteredResults = results.Any(a => (r1.IsMatch(a.Field)));
YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53
1

((keyword1)|(keyword2)|(keyword3))

Im not sure exactly how c# does regex but this should match and return matches.

You can test regexes here

kniteli
  • 491
  • 3
  • 10