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

re.sub

正则表达式

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

re模块

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

https://blog.csdn.net/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

https://blog.csdn.net/qq_44159028/article/details/120575621?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165804119916781647576974%2522%252C%2522scm%2522%253A%252220140713.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 leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the Match object and must return
    a 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 = “” 即为去除

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值