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是第一组。
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是第一组。