200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python 使用正则表达式去除常见标点符合

python 使用正则表达式去除常见标点符合

时间:2024-02-19 02:47:47

相关推荐

python 使用正则表达式去除常见标点符合

re.sub

正则表达式

正则表达式,又称规则表达式,(Regular Expression,在代码中常简写为regex、regexp或RE),是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"),是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式(规则)的文本。

re模块

Python通过标准库re模块来支持正则表达式,通过这个模块就可以使 Python 拥有全部的正则表达式功能,使用之前我们需要导入re模块

/weixin_43331684/article/details/102988442?ops_request_misc=&request_id=&biz_id=102&utm_term=%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8Fr%22%5B%25s%5D+%22%20%25&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-102988442.142v32down_rank,185v2control&spm=1018.2226.3001.4187

/qq_44159028/article/details/120575621?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165804119916781647576974%2522%252C%2522scm%2522%253A%25220713.130102334…%2522%257D&request_id=165804119916781647576974&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-2-120575621-null-null.142v32down_rank,185v2control&utm_term=%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8Fpython&spm=1018.2226.3001.4187

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个参数:

pattern:表示正则中的模式字符串;repl:表示要替换的字符串(即匹配到pattern后替换为repl),也可以是个函数;string:表示要被处理(查找替换)的原始字符串;count:可选参数,表示要替换的最大次数,而且必须是非负整数,该参数默认为0,即> 所有的匹配都会替换;flags:可选参数,表示编译时用的匹配模式(如忽略大小写、多行模式等),数字形式,默认为0。

repl = “” 即为去除

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。