re.sub()基本用法

re.sub()基本用法

re.sub()

sub=substitute(替换);
re.sub是个正则表达式替换函数,用来实现通过正则表达式,实现比普通字符串的replace更加强大的替换功能;
str.replace(“aaa”, “bbb”):将字符串str中的“aaa”替换为“bbb”

例子:
str="sajhaskdhj111sjh111dksd333"
str1=str.replace("111","222")
print(str)#sajhaskdhj111sjh111dksd333
print(str1)#sajhaskdhj222sjh222dksd333

re.sub(pattern, repl, string, count=0, flags=0)
pattern:模式串 正则表达式
repl:需要替换成的字符串
string:需要被替换的字符串
count:替换次数

猜你喜欢

转载自www.cnblogs.com/demiao/p/12821957.html