简体   繁体   中英

RegExp for required keywords?

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.

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

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)));

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

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

You can test regexes here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM