首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3重放负向不工作

Python3重放负向不工作
EN

Stack Overflow用户
提问于 2017-07-27 04:26:37
回答 1查看 29关注 0票数 0

我想检查在[a-zA-Z]之前没有attachment的字符串。

代码语言:javascript
复制
def is_attachment_url(url):
    """check url"""
    pattern = '(?<![\w]+)attachment'
    return re.search(pattern, url, re.I)


tests = (
    'article_?attachment',  # should be false
    'article_fattachment',  # should be false
    'article_-attachment',  # should be true
    'article_/attachment',  # should be true
)
for ss in tests:
    print(is_attachment_url(ss))

错误提示:

代码语言:javascript
复制
    raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-27 04:53:33

模式中的+使其变宽.您不需要它,因为您只想在“附件”之前检查单个字符,所以只需删除它:

pattern = '(?<![\w])attachment'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45341421

复制
相关文章

相似问题

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