sebastianwth
sebastianwth

Reputation: 187

Python regex match, but exclude if regex match

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.

How can i do this? Thanks.

Upvotes: 0

Views: 110

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

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

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

Demo

Upvotes: 2

Related Questions