1.第一个参数:pattern
pattern,表示正则表达式
反斜杠加数字(\N),则对应着匹配的组(matched group)
比如\6,表示匹配前面pattern中的第6个group
意味着,pattern中,前面肯定是存在对应的,第6个group,然后你后面也才能去引用
返回值: 如果匹配到对应组中的内容,怎返回repl
反斜杠加数字(\N)
In [1]: import re
In [2]: inputStr = "hello crifan, nihao crifan"
In [3]: replacedStr = re.sub(r"hello (\w+), nihao \1", "crifanli", inputStr)
In [4]: print ("replacedStr=",replacedStr) #crifanli
# 输出
replacedStr= crifanli
不加反斜杠加数字(\N)
In [5]: replacedStr = re.sub(r"hello (\w+), nihao", "crifanli", inputStr