我遇到了正則表達式標記化和Unicode字符串的一個奇怪問題。Unicode正則表達式的Python正則表達式標記不按預期工作
> mystring = "Unicode rägular expressions"
> tokens = re.findall(r'\w+', mystring, re.UNICODE)
這就是我得到:
> print tokens
['Unicode', 'r\xc3', 'gular', 'expressions']
這是我所期待的:
> print tokens
['Unicode', 'rägular', 'expressions']
我有什麼做的就是預期的結果?
更新:這個問題不同於我的: matching unicode characters in python regular expressions但它的回答https://stackoverflow.com/a/5028826/1251687也可以解決我的問題。
'\ w'不包含像ä一樣的Unicode字符。 – Xufox
那麼這樣做的方法是什麼? – boadescriptor
\ w如果您使用re.UNICODE,則包含unicode。 – Javier