我已经阅读了Stackoverflow中的其他问题,但仍然没有更接近。对不起,如果这已经得到解答,但我没有得到任何建议。在python正则表达式中匹配unicode字符
>>> import re
>>> m = re.match(r'^/by_tag/(?P<tag>\w+)/(?P<filename>(\w|[.,!#%{}()@])+)$', '/by_tag/xmas/xmas1.jpg')
>>> print m.groupdict()
{'tag': 'xmas', 'filename': 'xmas1.jpg'}
一切都很好,然后我尝试用挪威语字符的东西它(或更多的东西Unicode的等):
>>> m = re.match(r'^/by_tag/(?P<tag>\w+)/(?P<filename>(\w|[.,!#%{}()@])+)$', '/by_tag/påske/øyfjell.jpg')
>>> print m.groupdict()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groupdict'
我如何可以匹配典型的Unicode字符,如øæå?我希望能够匹配这些字符以及上面的标记组和文件名。
确保您[规范化](HTTPS: //docs.python.org/2/library/unicodedata.html#unicodedata.normalize)你的字符串,因为有不同的码点序列产生相同的视觉外观。 – janbrohl 2016-08-26 17:25:40