(作者:玛瑙河,转载请注明作者或出处,)
字符串替换
1.替换所有匹配的子串
1
#用newstring替换subject中所有与正则表达式regex匹配的子串
2
result = re.sub(regex, newstring, subject)

2

2.替换所有匹配的子串(使用正则表达式对象)
1
reobj = re.compile(regex)
2
result = reobj.sub(newstring, subject)

2

字符串拆分
1.字符串拆分
1
result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)
1
reobj = re.compile(regex)
2
result = reobj.split(subject)

2
