我正在尝试https://docs.python.org/2/library/re.html中的示例:
>>> import re
>>> re.sub("(.+) \1","found","55 55")
'55 55'
为什么我有“ 55 55”而不是“ found”? PS我使用python 3.7.2
PPS 我发现
>>> re.sub("(.+) \1","found","55 \1")
'found'
因此,它将'\ 1'视为简单文本。为什么?
答案 0 :(得分:0)
我发现了原因:正则表达式字符串之前应该有 r :
>>> re.sub(r"(.+) \1","found","55 55")
'found'