Umlauts in regexp matching (via locale?)

前端 未结 2 1994
粉色の甜心
粉色の甜心 2020-12-17 19:27

I\'m surprised that I\'m not able to match a German umlaut in a regexp. I tried several approaches, most involving setting locales, but up to now to no avail.



        
相关标签:
2条回答
  • 2020-12-17 20:01

    Have you tried to use the re.UNICODE flag, as described in the doc?

    >>> re.findall(r'\w+', 'abc def güi jkl', re.UNICODE)
    ['abc', 'def', 'g\xc3\xbci', 'jkl']
    

    A quick search points to this thread that gives some explanation:

    re.LOCALE just passes the character to the underlying C library. It really only works on bytestrings which have 1 byte per character. UTF-8 encodes codepoints outside the ASCII range to multiple bytes per codepoint, and the re module will treat each of those bytes as a separate character.

    0 讨论(0)
  • 2020-12-17 20:17

    In my case \S gave me better results than \w, plus saving the file as utf-8, plus using re.UNICODE

    0 讨论(0)
提交回复
热议问题