Reputation: 156
I have a string as
string = '''
making the regex for project
is the basic work '''
The regex library has to match if it contains 'project' and not 'work'.
I wrote it like this:
bool(re.search('project.*(?!work)'))
It is returning the true as it was found. But it has to return the false.
Upvotes: 1
Views: 303
Reputation: 627607
Placing a negative looksahead after a quantified pattern with a lazy or greedy quantifier almost never yields expected results because of backtracking: once the .*
matches 0+ chars other than line break chars it checks the lookahead pattern and since there is no work
right after the end of the first line, the result is true.
To fix the regex, you need to make sure .
matches line break chars (pass re.DOTALL
modifier or add (?s)
at the start) and move .*
to the lookahead:
re.search(r'project(?!.*work)', s, re.DOTALL)
See the regex demo.
This pattern means: find project
and then fail the match if there is check
substring after any 0+ chars.
Note that if you plan to use this regex as is, it might make more sense to use if 'project' in s and 'work' not in s
to check for the same thing.
Upvotes: 4