-1

I have to check text files content to see if it contains certain words (ex: hello, hi) but not contains somes (ex: bye, again):

dfadsfdsf hello sir,
good morning, how are you

Regex.IsMatch will return true but

hello sir,
good morning, how are you
hssss bye some 
some other

Regex.IsMatch will return false

Please help me with RegEx string.

Words can appear in any order and may not stand alone: bye can be byebye or byeabc.

I am using VS2019.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
QuangND
  • 67
  • 1
  • 10

1 Answers1

1

Two lookaheads should do it for you - one positive and one negative:

(?=.*\b(hi|hello)\b)(?!.*\b(bye|again)\b).*

(?= starts a positive lookahead looking for the words hi or hello. The \b, a word boundary anchor, makes sure it's not a part of a word that's found, like thick.

Then (?! is the negative lookahead, making sure the words bye and again aren't present.

When that's made certain, .* matches anything until the end.

And, as pointed out in a comment, You need to set the RegexOptions.Singleline option.

Edit: Obviously I hadn't read the question thoroughly enough :/. If you want matches on words where only a part is matched, remove the word boundary anchors \b.

SamWhan
  • 8,296
  • 1
  • 18
  • 45