我想在[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
答案 0 :(得分:0)
模式中的x
使其变宽。你不需要它,因为你只想在&#34;附件&#34;之前检查单个字符,所以只需删除它:
+