我已经读过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字符,如øå?我也希望能够在上面的标记组和filename的标记组中匹配这些字符。
您需要UNICODE标志:
您需要指定
re.UNICODE
标志,和使用u
前缀将字符串输入为Unicode字符串:这是在Python 2中;在Python 3中,必须省略
u
,因为所有字符串都是Unicode。在Python 2中,需要re.UNICODE标志和unicode字符串构造函数
(在后一种情况下,逗号是中文逗号。)
相关问题 更多 >
编程相关推荐