首页 > 解决方案 > 正则表达式用 Python 中的英文逗号替换中文标点符号

问题描述

标签: pythonregexpunctuation

解决方案


One way to do with re module

import re
str='上海,北京、武汉;重庆。欢迎你!你好'
s = re.sub(r'[^\w\s]',',',str)
print(s)

Output:

上海,北京,武汉,重庆,欢迎你,你好

Explanation,

[^\w\s]- Match a single character Not present in the list below-

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 ])

推荐阅读