使用Python 的re模块,re模块提供了re.sub用于替换字符串中的匹配项。

1 re.sub(pattern, repl, string, count=0)

参数说明:

pattern:正则重的模式字符串

repl:被拿来替换的字符串

string:要被用于替换的原始字符串

count:模式匹配后替换的最大次数,省略则默认为0,表示替换所有的匹配

例如

import re
str = "hello,world!!%[545]你好234世界。。。"
str = re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", str)
print(str)

输出结果:你好世界
Last modification:April 2nd, 2022 at 02:57 pm
如果觉得我的文章对你有用,请随意赞赏