我在其他文章中看到,在执行(?<!X|Y|Z)时,这是解决不固定宽度模式问题的正确方法,但并不适合我。
我正在尝试以下几种方法
re.search(r'\b(?:(?<!Yummy)|(?<!Xoo))\bfoo\b', "Yummy foo", flags=re.UNICODE) is not None => True
re.search(r'\b(?:(?<!Yummy)|(?<!Xoo))\bfoo\b', "Xoo foo", flags=re.UNICODE) is not None => True
re.search(r'\b(?:(?<!Yummy)|(?<!Xoo))\bfoo\b', "other Yummy foo someone", flags=re.UNICODE) is not None => True当它应该返回False时,它总是返回True。现在,如果我删除了or \\,它就会正常工作。
re.search(r'\b(?:(?<!Yummy))\bfoo\b', "Yummy foo", flags=re.UNICODE) is not None => False
re.search(r'\b(?<!Yummy)\bfoo\b', "Yummy foo", flags=re.UNICODE) is not None => False
re.search(r'\b(?<!Yummy)\bfoo\b', " Yummy foo", flags=re.UNICODE) is not None => False
re.search(r'\b(?<!Yummy)\bfoo\b', "other Yummy foo someone", flags=re.UNICODE) is not None => False
re.search(r'\b(?<!Yummy)\bfoo\b', "foo someone", flags=re.UNICODE) is not None => True
re.search(r'\b(?<!Yummy)\bfoo\b', "foo", flags=re.UNICODE) is not None => True有什么建议吗?
发布于 2014-07-12 15:03:36
您不需要使用替换,因为后面的查找是零宽度断言,您可以这样写:
re.search(r'\b(?<!\bYum)(?<!\bXooop)\s+foo\b', "Yum foo", flags=re.UNICODE)
^ ^ ^
| | |
+-+---------+---These three assertions are tested at the same
position (i.e. immediatly before the \s+ match.
You can put them in any order you want, they are
only checks and don't eat characters.https://stackoverflow.com/questions/24713868
复制相似问题