正则中的\1\2\3问题

import re

s = "Today is 3/2/2017 。Pycon starts 5/25/2017"

new_s = re.sub("(\d+)/(\d+)/(\d+)",r"\3-\1-\2",s)
print(new_s)

输出结果

Today is 2017-3-2 。Pycon starts 2017-5-25

首先\1\2\3要配合前面第几组括号使用,比如这里的\3就是匹配第三组括号,\2是第二组,\1是第一组。

猜你喜欢

转载自www.cnblogs.com/c-x-a/p/11002041.html