对于中文单词:上海,北京、武汉;重庆。欢迎你!你好,我想替换中文标点符号 带逗号,我如何在 Python 中使用正则表达式来做到这一点?
请您参考如下方法:
一种使用re模块的方法
import re
str='上海,北京、武汉;重庆。欢迎你!你好'
s = re.sub(r'[^\w\s]',',',str)
print(s)
输出:
上海,北京,武汉,重庆,欢迎你,你好
解释,
[^\w\s]- 匹配不出现在下面列表中的单个字符-
1. \w matches any word character (equal to [a-zA-Z0-9_])
2. \s matches any whitespace character (equal to [\r\n\t\f\v ])
