Regex for accent insensitive replacement in python

前端 未结 2 521
灰色年华
灰色年华 2021-01-12 15:07

In Python 3, I\'d like to be able to use re.sub() in an \"accent-insensitive\" way, as we can do with the re.I flag for case-insensitive substituti

2条回答
  •  长情又很酷
    2021-01-12 15:43

    You can use Unidecode:

    $ pip install unidecode
    

    In your program:

    from unidecode import unidecode
    
    original_text = "I'm drinking a café in a cafe."
    unidecoded_text = unidecode(original_text)
    regex = r'cafe'
    re.sub(regex, 'X', unidecoded_text)
    

提交回复
热议问题