当前位置: 首页 > news >正文

re.sub()用法的详细介绍

一、前言

在字符串数据处理的过程中,正则表达式是我们经常使用到的,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?——"

文章来源于https://blog.csdn.net/jackandsnow/article/details/103885422

http://www.lryc.cn/news/22908.html

相关文章:

  • 【Python数据挖掘入门】2.2文本分析-中文分词(jieba库cut方法/自定义词典load_userdict/语料库分词)
  • Meta利用视觉信息来优化3D音频模型,未来将用于AR/VR
  • openlayers加载离线地图并实现深色地图
  • socket,tcp,http三者之间的区别和原理
  • 红日(vulnstack)1 内网渗透ATTCK实战
  • ik 分词器怎么调用缓存的词库
  • ROS1/2机器人操作系统与时间Time的不解之缘
  • 华为OD机试真题2022(JAVA)
  • 【3】MyBatis+Spring+SpringMVC+SSM整合一套通关
  • 20道前端高频面试题(附答案)
  • android EditText设置后缀
  • prometheus+cadvisor监控docker
  • 正演(1): 二维声波正演模拟程序(中心差分)Python实现
  • 珠海数据智能监控器+SaaS平台 轻松实现SMT生产管控
  • 习题22对前面21节的归纳总结
  • 使用Vite快速构建前端React项目
  • 人工智能高等数学--人工智能需要的数学知识_微积分_线性代数_概率论_最优化---人工智能工作笔记0024
  • 阿里大数据之路总结
  • ABAP中Literals的用法(untyped literal vs. typed literal)
  • tensorflow1.14.0安装教程
  • C++赋值运算符重载
  • 网络性能总不好?专家帮你来“看看”— CANN 6.0 黑科技 | 网络调优专家AOE,性能效率双提升
  • Qss自定义属性
  • 连接金蝶云星空,数据交互轻松搞定!丨三叠云
  • JSX是什么,React为什么使用JSX,babel怎么转译JSX的
  • 从工地转行软件测试,拿下13k+年终奖是种什么体验?
  • 前端面试题 —— 计算机网络(二)
  • 山东大学机器学习期末2022
  • FEBC2022|打造VR内容生态闭环 佳创视讯持续加码轻量化内容建设
  • Redis常见的数据类型命令