试图让re或regex将非ASCII字符串与自身匹配。我阅读了其他关于非ASCII/unicode的帖子,并尝试添加unicode标志,但没有效果:
# python
Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> import regex
>>> s1 = 'wow'
>>> s2 = 'ℛℯα∂α♭ℓℯ ♭ʊ☂ η☺т Ѧ$☾ℐℐ'
>>> print(s2)
ℛℯα∂α♭ℓℯ ♭ʊ☂ η☺т Ѧ$☾ℐℐ
>>> re.search(s1,s1)
<_sre.SRE_Match object at 0x7f0ce27c38b8>
>>> re.search(s2,s2)
>>> type(s2)
<type 'str'>
>>> us2 = unicode(s2,'utf-8')
>>> us2
u'\u211b\u212f\u03b1\u2202\u03b1\u266d\u2113\u212f \u266d\u028a\u2602 \u03b7\u263a\u0442 \u0466$\u263e\u2110\u2110'
>>> re.search(us2,us2,re.UNICODE)
>>> regex.search(s2,s2)
>>> regex.search(us2,us2,regex.UNICODE)
>>>
我希望我遗漏了一些明显的东西。非常感谢您的帮助!你知道吗
注意,作为regex模式,
s2
内部有一个at at_end
模式。你知道吗这是因为,作为utf-8编码字符串,
s2
是注意在
s2
中有一个$
:要防止
$
被解释为at at_end
模式,请使用re.escape
转义模式中的所有非字母数字字符:转义unicode模式也是如此:
自
相关问题 更多 >
编程相关推荐