我已经阅读了 Stackoverflow 上的其他问题,但仍然没有接近。对不起,如果这已经得到回答,但我没有得到任何建议在那里工作。
>>> 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 字符,例如 øæå?我也希望能够在上面的标签组和文件名中匹配这些字符。