문제

I want to have a word boundary within a negative lookahead. However, as you can see in the example below this does not appear to work in Python. Is this unsupported? If so, is there a way workaround?

To state the full problem I am trying to solve: I have a regex that I'm using in re.sub, and there are a couple specific words (like "455") that I want to specifically not match.

In [8]: print re.match('(?!455)455', '455')
None

In [9]: print re.match('(?!455\b)455', '455')
<_sre.SRE_Match object at 0x1108fb440>
도움이 되었습니까?

해결책

\b is an escape sequence, and as such it will match the backslash character ASCII 0x08. You need to escape the backslash or use raw string literals:

>>> print(re.match(r'(?!455\b)455', '455'))
None
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top