对于中文单词:上海,北京、武汉;重庆。欢迎你!你好
,我想替换中文标点符号
带逗号,我如何在 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 ])
关于python - 正则表达式在Python中用英文逗号替换中文标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56896099/