来自https://docs.python.org/2/library/re.html#regular-expression-syntax :
(?!...)
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'.
但是如果Isaac和Asimov之间有一个或多个单词怎么办?
最佳答案
Isaac(?!.*Asimov)
对以 Asimov 结尾的任何字符序列使用负前瞻。如果您想在 .
中包含换行符,[\s\S]
或 DOTALL
标志可能比较合适。
关于Python 正则表达式 : how to do a negative lookahead with a string which don't start immediately?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946398/