0

I'm trying to write a regex for re.search that can match a string as long as the word prod exists anywhere in the text (.*prod.*). however, it should fail the match if the string should have the word orange anywhere in it.

  • web-prod-green # should match
  • web-prod-orange # should fail to match
  • web-orange-green # should fail to match
  • orange-prod-green # should fail to match

How can i do this? Thanks.

1
  • 1
    Why do you want to use regex? Why not just if "prod" in text and "orange" not in text? Commented Dec 11, 2022 at 8:46

1 Answer 1

2

We could use a negative lookahead to exclude the presence of orange:

^(?!.*\borange\b).*\bprod\b.*$

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks tim. Is there an or operator where I can say either orange, or green or blue.
For orange, green, or blue, use: ^(?!.*\b(?:orange|green|blue)\b).*\bprod\b.*$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.