首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Regex回顾问题

Regex回顾问题
EN

Stack Overflow用户
提问于 2014-07-12 14:25:51
回答 1查看 52关注 0票数 0

我在其他文章中看到,在执行(?<!X|Y|Z)时,这是解决不固定宽度模式问题的正确方法,但并不适合我。

我正在尝试以下几种方法

代码语言:javascript
复制
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 \\,它就会正常工作。

代码语言:javascript
复制
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

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-12 15:03:36

您不需要使用替换,因为后面的查找是零宽度断言,您可以这样写:

代码语言:javascript
复制
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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24713868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档