Python3重新预测否定不起作用

时间:2017-07-27 04:26:37

标签: python-3.x

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

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))

错误提示:

    raise error("look-behind requires fixed-width pattern")
sre_constants.error: look-behind requires fixed-width pattern

1 个答案:

答案 0 :(得分:0)

模式中的x使其变宽。你不需要它,因为你只想在&#34;附件&#34;之前检查单个字符,所以只需删除它:

+