Reputation: 401
I need to match for a pattern not check
in a bigger string.
bigger_string = '<some words and> do not check <some other words>'
But it shouldn't have a number after that. So, not check <some words>
should match but not check 67 <some words>
shouldn't match.
I tried these:
re.findall(re.compile(r'not\s*check\s*\D*', re.I), bigger_string)
re.findall(re.compile(r'not\s*check\s*[^0-9]*', re.I), bigger_string)
It doesn't work. This always returns a match.
Upvotes: 2
Views: 72
Reputation: 18611
\D*
in not\s*check\s*\D*
means match up to the first digit, not match ony if there is no digit ahead.
Use
\bnot\s+check\b(?!\s*\d)\D*
See proof.
EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
not 'not'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
check 'check'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\D* non-digits (all but 0-9) (0 or more times
(matching the most amount possible))
Upvotes: 1