user4316384
user4316384

Reputation:

Regex match if it has a word and other word

I'm trying to write a regular expression that matches if the string has a word, "worker", and doesn't have another, "TextNotification".

I have an example here with what I've been working: http://rubular.com/r/sjYWePWk0s

In this case, out of the four lines, I should only get matched the first one.

Upvotes: 1

Views: 50

Answers (2)

Kusalananda
Kusalananda

Reputation: 15613

Do this as two separate tests.

  1. Test for worker. If that test succeeds, then
  2. Test for TextNotification. If that test fails, then
  3. You have your data.

Trying to cram everything into one single regular expression is often

  1. Prone to errors.
  2. Unreadable.

Upvotes: 0

anubhava
anubhava

Reputation: 785126

You are close. Use this regex with lookahead before actual match:

^(?!.*TextNotification).*worker.*$

RegEx Demo

Upvotes: 1

Related Questions