Search code examples
c#regexlinqpattern-matchingphrase

regex for phrase searching


I have to search phrase in big string may be length of 500 or 600 or greater now I have to check whether phrase exist or not

 phrase =  "Lucky Draw"

big string1  = "I'm looking for Lucky Draw a way to loop through the sentences and check"

big string1  = "I'm looking for  specialLucky Draw a way to loop through the sentences and check"

big string3  = "I'm looking for Lucky Draws a way to loop through the sentences and check"

bool success = Regex.Match(message, @"\bLucky Draw\b").Success;

I am doing the above workaround but it does not satisfy all cases.

what I have to do when I have multiple phrases, I want to use linq in that case like :

bool success = Regex.Match(message,  **arrayofstrings**).Success;

Solution

  • You can use a loop to build one large regex of \b(phrase one|phrase two|phrase three|etc)\b from your array of phrases and then use that regex to match against your strings.