re.sub()用法的详细介绍
目录
- 一、前言
- 二、函数原型
- 三、使用案例
- 1.匹配单个数字或字母
- 2.匹配多个数字或字母
- 3.匹配其他
- 四、致谢
一、前言
在字符串数据处理的过程中,正则表达式是我们经常使用到的,python中使用的则是re模块。下面会通过实际案例介绍 re.sub() 的详细用法,该函数主要用于替换字符串中的匹配项。
二、函数原型
首先从源代码来看一下该函数原型,包括各个参数及其意义:
def sub(pattern, repl, string, count=0, flags=0):"""Return the string obtained by replacing the leftmostnon-overlapping occurrences of the pattern in string by thereplacement repl. repl can be either a string or a callable;if a string, backslash escapes in it are processed. If it isa callable, it's passed the match object and must returna replacement string to be used."""return _compile(pattern, flags).sub(repl, string, count)
从上面的代码中可以看到re.sub()方法中含有5个参数,下面进行一一说明(加粗的为必须参数):
(1)pattern:该参数表示正则中的模式字符串;
(2)repl:该参数表示要替换的字符串(即匹配到pattern后替换为repl),也可以是个函数;
(3)string:该参数表示要被处理(查找替换)的原始字符串;
(4)count:可选参数,表示是要替换的最大次数,而且必须是非负整数,该参数默认为0,即所有的匹配都会被替换;
(5)flags:可选参数,表示编译时用的匹配模式(如忽略大小写、多行模式等),数字形式,默认为0。
三、使用案例
下面将以一个字符串(包含大小写英文、数字、中英文标点、特殊符号等)作为示例进行使用案例讲解,该字符串如下:
>>> s = "大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn"
1.匹配单个数字或字母
(1)只匹配单一数字
>>> import re>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9]', '*', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m ** years old. Today is ****/**/**. It is a wonderful DAY! @HHHHello,,,#***ComeHere***...**?AA?zz?——http://welcome.cn"
上面 re.sub(r'[0-9]', '*', s)
这句话则表示只匹配单一数字,并将每一个数字替换为一个星号 。
(2)只匹配单一字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[a-z]', '*', s)"大家好,我是一个程序员小白。I '* ** **** ** ********* ******, *** I’* 18 ***** ***. T**** ** 2020/01/01. I* ** * ********* DAY! @HHHH****,,,#111C***H***222...66?AA?**?——****://*******.**">>> re.sub(r'[A-Z]', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m 18 years old. *oday is 2020/01/01. *t is a wonderful ***! @****ello,,,#111*ome*ere222...66?**?zz?——http://welcome.cn">>> re.sub(r'[A-Za-z]', '*', s)"大家好,我是一个程序员小白。* '* ** **** ** ********* ******, *** *’* 18 ***** ***. ***** ** 2020/01/01. ** ** * ********* ***! @********,,,#111********222...66?**?**?——****://*******.**"
上面 re.sub(r'[a-z]', '*', s)
这句话则表示只匹配单一小写字母,并将每一个小写字母替换为一个星号 。
上面 re.sub(r'[A-Z]', '*', s)
这句话则表示只匹配单一大写字母,并将每一个大写字母替换为一个星号 。
上面 re.sub(r'[A-Za-z]', '*', s)
这句话则表示只匹配单一字母,并将每一个字母替换为一个星号 。
(3)匹配单一数字和字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9A-Z]', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m ** years old. *oday is ****/**/**. *t is a wonderful ***! @****ello,,,#****ome*ere***...**?**?zz?——http://welcome.cn">>> re.sub(r'[0-9a-z]', '*', s)"大家好,我是一个程序员小白。I '* ** **** ** ********* ******, *** I’* ** ***** ***. T**** ** ****/**/**. I* ** * ********* DAY! @HHHH****,,,#***C***H******...**?AA?**?——****://*******.**">>> re.sub(r'[0-9A-Za-z]', '*', s)"大家好,我是一个程序员小白。* '* ** **** ** ********* ******, *** *’* ** ***** ***. ***** ** ****/**/**. ** ** * ********* ***! @********,,,#**************...**?**?**?——****://*******.**"
上面 re.sub(r'[0-9A-Z]', '*', s)
这句话则表示只匹配单一数字和大写字母,并将每一个数字和大写字母替换为一个星号 。
上面 re.sub(r'[0-9a-z]', '*', s)
这句话则表示只匹配单一数字和小写字母,并将每一个数字和小写字母替换为一个星号 。
上面 re.sub(r'[0-9A-Za-z]', '*', s)
这句话则表示只匹配单一数字和字母,并将每一个数字和字母替换为一个星号 。
2.匹配多个数字或字母
注意:这里的所说的多个指的是大于等于一个。
(1)匹配多个数字
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9]+', '*', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m * years old. Today is */*/*. It is a wonderful DAY! @HHHHello,,,#*ComeHere*...*?AA?zz?——http://welcome.cn"
上面 re.sub(r'[0-9]+', '*', s)
这句话则表示匹配多个连续的数字,并将多个连续的数字替换为一个星号 。
(2)匹配多个字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[a-z]+', '*', s)"大家好,我是一个程序员小白。I '* * * * * *, * I’* 18 * *. T* * 2020/01/01. I* * * * DAY! @HHHH*,,,#111C*H*222...66?AA?*?——*://*.*">>> re.sub(r'[A-Z]+', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m 18 years old. *oday is 2020/01/01. *t is a wonderful *! @*ello,,,#111*ome*ere222...66?*?zz?——http://welcome.cn">>> re.sub(r'[a-zA-Z]+', '*', s)"大家好,我是一个程序员小白。* '* * * * * *, * *’* 18 * *. * * 2020/01/01. * * * * *! @*,,,#111*222...66?*?*?——*://*.*"
上面 re.sub(r'[a-z]+', '*', s)
这句话则表示匹配多个连续的小写字母,并将多个连续的小写字母替换为一个星号 。
上面 re.sub(r'[A-Z]+', '*', s)
这句话则表示匹配多个连续的大写字母,并将多个连续的大写字母替换为一个星号 。
上面 re.sub(r'[A-Za-z]+', '*', s)
这句话则表示匹配多个连续的字母,并将多个连续的字母替换为一个星号 。
(3)匹配多个数字和字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9a-zA-Z]+', '*', s)"大家好,我是一个程序员小白。* '* * * * * *, * *’* * * *. * * */*/*. * * * * *! @*,,,#*...*?*?*?——*://*.*"
上面 re.sub(r'[0-9A-Za-z]+', '*', s)
这句话则表示匹配多个连续的数字和字母,并将多个连续的数字、连续的字母、连续的数字和字母替换为一个星号 。
3.匹配其他
(1)匹配非数字
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^0-9]', '*', s)'********************************************************18***********************2020*01*01**************************************111********222***66**************************'>>> re.sub(r'[^0-9]+', '*', s)'*18*2020*01*01*111*222*66*'
上面 re.sub(r'[^0-9]', '*', s)
这句话则表示匹配单个非数字,并将单个非数字替换为一个星号 。
上面 re.sub(r'[^0-9]+', '*', s)
这句话则表示匹配多个连续的非数字,并将多个连续的非数字替换为一个星号 。
(2)匹配非字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^a-z]', '*', s)'*****************m*so*glad*to*introduce*myself**and***m****years*old*****oday*is**************t*is*a*wonderful***********ello********ome*ere************zz***http***welcome*cn'>>> re.sub(r'[^A-Z]', '*', s)'**************I*************************************I*******************T********************I*****************DAY***HHHH***********C***H************AA***********************'>>> re.sub(r'[^A-Za-z]', '*', s)'**************I**m*so*glad*to*introduce*myself**and*I*m****years*old****Today*is*************It*is*a*wonderful*DAY***HHHHello*******ComeHere*********AA*zz***http***welcome*cn'>>> re.sub(r'[^a-z]+', '*', s)'*m*so*glad*to*introduce*myself*and*m*years*old*oday*is*t*is*a*wonderful*ello*ome*ere*zz*http*welcome*cn'>>> re.sub(r'[^A-Z]+', '*', s)'*I*I*T*I*DAY*HHHH*C*H*AA*'>>> re.sub(r'[^A-Za-z]+', '*', s)'*I*m*so*glad*to*introduce*myself*and*I*m*years*old*Today*is*It*is*a*wonderful*DAY*HHHHello*ComeHere*AA*zz*http*welcome*cn'
上面 re.sub(r'[^a-z]', '*', s)
这句话则表示匹配单个非小写字母,并将单个非小写字母替换为一个星号 。
上面 re.sub(r'[^A-Z]', '*', s)
这句话则表示匹配单个非大写字母,并将单个非大写字母替换为一个星号 。
上面 re.sub(r'[^A-Za-z]', '*', s)
这句话则表示匹配单个非字母,并将单个非字母替换为一个星号 。
上面 re.sub(r'[^a-z]+', '*', s)
这句话则表示匹配多个连续的非小写字母,并将多个连续的非小写字母替换为一个星号 。
上面 re.sub(r'[^A-Z]+', '*', s)
这句话则表示匹配多个连续的非大写字母,并将多个连续的非大写字母替换为一个星号 。
上面 re.sub(r'[^A-Za-z]+', '*', s)
这句话则表示匹配多个连续的非字母,并将多个连续的非字母替换为一个星号 。
(3)匹配非数字和非字母
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^0-9A-Za-z]', '*', s)'**************I**m*so*glad*to*introduce*myself**and*I*m*18*years*old****Today*is*2020*01*01**It*is*a*wonderful*DAY***HHHHello****111ComeHere222***66*AA*zz***http***welcome*cn'>>> re.sub(r'[^0-9A-Za-z]+', '*', s)'*I*m*so*glad*to*introduce*myself*and*I*m*18*years*old*Today*is*2020*01*01*It*is*a*wonderful*DAY*HHHHello*111ComeHere222*66*AA*zz*http*welcome*cn'
上面 re.sub(r'[^0-9A-Za-z]', '*', s)
这句话则表示匹配单个非数字和非字母,并将单个非数字和非字母替换为一个星号 。
上面 re.sub(r'[^0-9A-Za-z]+', '*', s)
这句话则表示匹配多个连续的非数字和非字母,并将多个连续的非数字和非字母替换为一个星号 。
(4)匹配固定形式
a.只保留字母和空格,将 repl 设置为空字符即可。
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^a-z ]', '', s)' m so glad to introduce myself and m years old oday is t is a wonderful elloomeerezzhttpwelcomecn'>>> re.sub(r'[^a-z ]+', '', s)' m so glad to introduce myself and m years old oday is t is a wonderful elloomeerezzhttpwelcomecn'>>> re.sub(r'[^A-Za-z ]', '', s)'I m so glad to introduce myself and Im years old Today is It is a wonderful DAY HHHHelloComeHereAAzzhttpwelcomecn'>>> re.sub(r'[^A-Za-z ]+', '', s)'I m so glad to introduce myself and Im years old Today is It is a wonderful DAY HHHHelloComeHereAAzzhttpwelcomecn'
如果要使句子语义和结构更完整,则要先将其余字符替换为空格(即repl设置为空格),然后去除多余的空格,如下:
>>> s1 = re.sub(r'[^A-Za-z ]+', ' ', s)>>> s1' I m so glad to introduce myself and I m years old Today is It is a wonderful DAY HHHHello ComeHere AA zz http welcome cn'>>> re.sub(r'[ ]+', ' ', s1)' I m so glad to introduce myself and I m years old Today is It is a wonderful DAY HHHHello ComeHere AA zz http welcome cn'
b.去除以 @ 开头的英文单词
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'@[A-Za-z]+', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! ,,,#111ComeHere222...66?AA?zz?——http://welcome.cn"
c.去除以 ?结尾的英文单词和数字(注意这是中文问号)
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[A-Za-z]+?', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?——http://welcome.cn">>> re.sub(r'[0-9A-Za-z]+?', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...——http://welcome.cn"
d.去除原始字符串中的URL
>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'http[:.]+\S+', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old. Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——"
四、致谢
以上就是通过实际案例对 re.sub() 用法的详细介绍,感谢大家的阅读,如果你觉得笔者写得不错,记得点个赞哦~ 当然,如果有问题,也可以在下方留言哦~
re.sub()用法的详细介绍相关推荐
- mysql raiserror_sql server数据库中raiserror函数用法的详细介绍
sql server数据库中raiserror函数的用法 server数据库中raiserror的作用就和asp.NET中的throw new Exception一样,用于抛出一个异常或错误.这个错误 ...
- 关于数据库having的用法的详细介绍
转自:微点阅读 https://www.weidianyuedu.com 数据库having的用法的用法你知道吗?下面小编就跟你们详细介绍下数据库having的用法的用法,希望对你们有用. 数据库h ...
- python or的用法_详细介绍Python中and和or实际用法
and 和 or 的特殊性质 在Python 中,and 和 or 执行布尔逻辑演算,但是它们并不返回布尔值:而是,返回它们实际进行比较的值之一.下面来看一下实例.>>> 'a' a ...
- java选择语句中switch的用法(详细介绍)
一.什么时候用switch? 在java中控制流程语句是由选择语句.循环语句.跳转语句构成.选择语句包括 if 和 switch,在过多的使用 if 语句嵌套会使程序很难阅读,这时利用 switch ...
- Eigen和Sophus 用法的详细介绍
一.Eigen 提供了快速的矩阵线性代数运算,矩阵.向量.旋转矩阵.变换矩阵. Eigen是纯头文件搭建成的库,使用时只需引入Eigen的头文件即可,不需要链接库文件target_link. #inc ...
- StringUtils常用方法+StringUtils详细介绍
StringUtils用法+StringUtils详细介绍 博文来源:http://yijianfengvip.blog.163.com/blog/static/1752734322012122219 ...
- php中sisson用法,详细介绍php中session的用法
PHP中的session默认情况下是使用客户端的Cookie.当客户端的Cookie被禁用时,会自动通过Query_String来传递. Php处理会话的函数一共有11个,我们详细介绍一下将要用到几个 ...
- 数据库having的用法详细介绍
转自:微点阅读 https://www.weidianyuedu.com 数据库having的用法的用法你知道吗?下面微点阅读小编就跟你们详细介绍下数据库having的用法的用法,希望对你们有用. ...
- 【MADDPG(MPE)——环境配置与用法详细介绍(多智能体强化学习))】
MADDPG(MPE)--环境配置与用法详细介绍(多智能体强化学习) MADDPG(MPE) 介绍 MPE环境安装教程 前期准备 MPE 安装包介绍 MPE 安装环境要求 开始安装 环境测试 MPE环 ...
- html table colgroup,关于HTML colgroup标签的用法你知道哪些?colgroup用法和col的详细介绍...
html中colgroup是什么意思?HTML colgroup标签的用法你又知道哪些?在这里,本篇文章就为大家介绍了关于HTML colgroup标签的用法详解,还有colgroup和col元素的详 ...
最新文章
- Android使用SQLite数据库(1)
- AngularJs 基础教程 —— 依赖注入
- python变量类型-Python 变量类型详解
- linux 脚本continue,shell中的break和continue
- 如何删除链表的最后一个节点_面试:删除链表的节点
- DelphiBCB一线程序员开发经验
- 【linux】io_uring 和 eBPF 将如何彻底改变 Linux 中的编程
- java 账户和密码 3次_模拟登录,给三次机会,并提示还有几次。Java实现
- 基于spring多数据源动态调用及其事务处理
- win7 更新失败解决方案一
- k8s部署应用nginx
- 高级软考之——系统分析师思维导图(一)
- 普元EOS的项目部署
- 【软考 系统架构设计师】软件架构设计⑦ 构件与中间件技术
- java 502错误_nginx 502 超时错误解决(java版本)
- iOS 开发中遇到的各种证书的详细说明以及申请讲解
- firefox插件使用:hackbar
- 2022面试经验分享(非技术)
- 家用服务器 无线路由器,评测六款热门家用Wi-Fi 6路由器
- SAP_HCM_Schema_CN28_XIN0