正则表达式后视问题

时间:2014-07-12 14:25:52

标签: python regex

我在其他帖子中看到,这是在执行(?<!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。现在,如果我删除或|,它可以正常工作。

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

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你不需要使用替换,因为lookbehinds是零宽度断言,你可以这样写:

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.