我想检查在[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发布于 2017-07-27 04:53:33
模式中的+使其变宽.您不需要它,因为您只想在“附件”之前检查单个字符,所以只需删除它:
pattern = '(?<![\w])attachment'
https://stackoverflow.com/questions/45341421
复制相似问题