import re
a = re.compile('^a(?!abc)')
b = re.compile('^a(?!bac)$')
c = re.compile('^a((?!ba).)*$')
a1 = "abbc"
print(a.search(a1)) # <re.Match object; span=(0, 1), match='a'>
print(b.search(a1)) # None
print(c.search(a1)) # <re.Match object; span=(0, 4), match='abbc'>
I don't understand the expression "?!" in python Regular expression.
Why is only a
searched in the first case?
I wonder if all the characters of abbc
can be searched because it is a match that the string abc
does not come next.
Why is b
none?
Doesn't it all apply to abbc
since it starts with a
and doesn't end with bac
?
I don't understand the expression "?!" in python Regular expression
Well, ?!
is not an expression, regular or otherwise.
The documentation says:
(?=...)
Matches if
...
matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example,Isaac (?=Asimov)
will match'Isaac '
only if it’s followed by'Asimov'
.
(?!...)
Matches if
...
doesn’t match next. This is a negative lookahead assertion. For example,Isaac (?!Asimov)
will match'Isaac '
only if it’s not followed by'Asimov'
.
(including the definition of a positive lookahead assertion for comparison).
So, the expression is really (?!...)
.
Note that the (?
sequence is used for several extensions to plain regular expressions, and they're all documented.
Why is only a searched in the first case?
Because a
is the only character the pattern asks for. The lookahead group could prevent a
from matching (if it was followed by abc
), but it wouldn't be part of the match either way.