3

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;
Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Usman Asif
  • 320
  • 2
  • 12
  • What cases does it not match? Do you want to match the `specialLucky Draw` in your second example? - If so its pretty clear you cannot use a boundary ... – Alex K. Jun 28 '18 at 15:19
  • yes you are right i have already observed this now i just want to do this using linq ** Regex.Match(message, arrayofstring).Success** – Usman Asif Jun 28 '18 at 15:31
  • Try `(?<=\s)Lucky\sDraw` to avoid matching `specialLucky Draw` – Paolo Jun 28 '18 at 15:32
  • can you please answer the second part – Usman Asif Jun 28 '18 at 15:39
  • You can use LINQ to do multiple tests - why use Regex if you are just searching for a matching substring? Please update your question with the correct answers to the comments. `arrayOfStrings.Any(s => message.Contains(s))`. – NetMage Jun 28 '18 at 18:48

1 Answers1

6

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.

blhsing
  • 91,368
  • 6
  • 71
  • 106