匹配python正则表达式中的unicode字符 [英] matching unicode characters in python regular expressions
问题描述
我已经阅读了 Stackoverflow 上的其他问题,但仍然没有更深入的了解.对不起,如果这已经得到回答,但我没有得到任何建议在那里工作.
<预><代码>>>>进口重新>>>m = re.match(r'^/by_tag/(?P一切都很好,然后我尝试了一些带有挪威语字符的东西(或者更像 unicode 的东西):
<预><代码>>>>m = re.match(r'^/by_tag/(?P如何匹配典型的 unicode 字符,例如 øæå?我也希望能够在上面的标签组和文件名的标签组中匹配这些字符.
您需要指定 re.UNICODE
标志,和将您的字符串作为 Unicode 字符串输入使用 u
前缀:
这是在 Python 2 中;在 Python 3 中,您必须省略 u
,因为所有字符串都是 Unicode.
I have read thru the other questions at Stackoverflow, but still no closer. Sorry, if this is allready answered, but I didn`t get anything proposed there to work.
>>> 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'}
All is well, then I try something with Norwegian characters in it ( or something more unicode-like ):
>>> 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'
How can I match typical unicode characters, like øæå? I`d like to be able to match those characters as well, in both the tag-group above and the one for filename.
You need to specify the re.UNICODE
flag, and input your string as a Unicode string by using the u
prefix:
>>> re.match(r'^/by_tag/(?P<tag>\w+)/(?P<filename>(\w|[.,!#%{}()@])+)$', u'/by_tag/påske/øyfjell.jpg', re.UNICODE).groupdict()
{'tag': u'p\xe5ske', 'filename': u'\xf8yfjell.jpg'}
This is in Python 2; in Python 3 you must leave out the u
because all strings are Unicode.
这篇关于匹配python正则表达式中的unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!