首页 > 编程语言 > 详细

Python:re.sub()实现字符串替换

时间:2018-03-06 16:25:53      阅读:608      评论:0      收藏:0      [点我收藏+]

1.re.sub的功能

re是regular expression的缩写,表示正则表达式

sub是substitude的缩写,表示替换

re.sub是正则表达式的函数,实现比普通字符串更强大的替换功能

eg:

replace 对比re.sub

>>>stri="hello 111 world 111"

>>>replaceStr=stri.replace("111", "222")       ===>"hello 222 world 222"

如果

>>>stri="hello 123 world 456"

>>>replacedStr = re.sub("\d+", "222", inputStr)   ===>把123和456,都换成222

2.re.sub的各个参数的详细解释

re.sub共有五个参数。

re.sub(pattern, repl, string, count=0, flags=0)  其中三个必选参数:pattern, repl, string   两个可选参数:count, flags

 

Python:re.sub()实现字符串替换

原文:https://www.cnblogs.com/baxianhua/p/8514679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!