Search code examples
pythonregexpunctuation

Regex to replace chinese punctuation with English comma in Python


For Chinese words: 上海,北京、武汉;重庆。欢迎你!你好, I want to replace Chinese punctuation with comma, how can I do that using regex in Python?


Solution

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