无法在 Python 2.7 re 中编译 8 位 Unicode 正则表达式范围 [英] Cannot compile 8 digit unicode regex ranges in Python 2.7 re

查看:49
本文介绍了无法在 Python 2.7 re 中编译 8 位 Unicode 正则表达式范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 2.7,重新

我正在尝试编译 unicode 字符类.我可以让它与 4 位数字范围 (u'\uxxxx') 一起工作,但不能使用 8 位数字 (u'\Uxxxxxxxx').I

以下工作:

re.compile(u'[\u0010-\u0012]')

以下没有:

re.compile(u'[\U00010000-\U00010001]')

由此产生的错误是:

<块引用>

回溯(最近一次调用最后一次):文件",第 1 行,在编译中的文件C:\Python27\lib\re.py",第 190 行返回_compile(模式,标志)_compile 中的文件C:\Python27\lib\re.py",第 242 行引发错误,v # 无效的表达式错误:字符范围错误

这似乎是 8 位范围的问题,因为以下工作正常:

re.compile(u'\U00010000')

单独的问题,我是 stackoverflow 的新手,我真的很想知道如何发布问题.我希望 Trackback 出现在多行上,而不是一行上.我还希望能够粘贴从解释器复制的内容,但是这个 UI 把>>>"弄得一团糟

不知道如何在评论编辑问题中添加此内容.

我真正想编译的表达式是:

re.compile(u'[\U00010000-\U0010FFFF]')

就扩展建议的解决方法而言,使用 list(u'[\U00010000-\U0010FFFF]') 扩展它看起来非常棘手:

<预><代码>>>>列表(u'[\U00010000-\U0010FFFF]')[u'[', u'\ud800', u'\udc00', u'-', u'\udbff', u'\udfff', u']']

解决方案

根据编译选项的不同,Python 2 可能会将 Unicode 字符串存储为 UTF-16 代码单元,因此 \U00010000 实际上是一个两个代码单元字符串:

<预><代码>>>>列表(u'[\U00010000-\U00010001]')[u'[', u'\ud800', u'\udc00', u'-', u'\ud800', u'\udc01', u']']

正则表达式解析器因此看到包含 \udc00-\ud800 的字符类,这是一个坏字符范围".在这个设置中,我想不出除了明确匹配代理对之外的解决方案(在确保 sys.maxunicode == 0xffff 之后):

<预><代码>>>>r = re.compile(u'\ud800[\udc00-\udc01]')>>>r.match(u'\U00010000')<_sre.SRE_Match 对象在 0x10cf6f440>>>>r.match(u'\U00010001')<_sre.SRE_Match 对象在 0x10cf4ed98>>>>r.match(u'\U00010002')>>>r.match(u'\U00020000')

Using Python 2.7, re

I'm trying to compile unicode character classes. I can get it to work with 4 digit ranges (u'\uxxxx') but not 8 digits (u'\Uxxxxxxxx').I

The following works:

re.compile(u'[\u0010-\u0012]')

The following does not:

re.compile(u'[\U00010000-\U00010001]')

The resultant error is:

Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\re.py", line 190, in compile return _compile(pattern, flags) File "C:\Python27\lib\re.py", line 242, in _compile raise error, v # invalid expression error: bad character range

It appears to be an issue with 8 digit ranges only as the following works:

re.compile(u'\U00010000')

Separate question, I am new to stackoverflow and I am really struggling with how to post questions. I would expect that Trackback to appear on multiple lines, not on one line. I would also like to be able to paste in content copied from the interpreter but this UI makes a mess out of '>>>'

Don't know how to add this in a comment editing question.

The expression I really want to compile is:

re.compile(u'[\U00010000-\U0010FFFF]')

Expanding it with list(u'[\U00010000-\U0010FFFF]') looks pretty intractable as far as extending the suggested workaround:

>>> list(u'[\U00010000-\U0010FFFF]')
[u'[', u'\ud800', u'\udc00', u'-', u'\udbff', u'\udfff', u']']

解决方案

Depending on the compilation option, Python 2 may store Unicode strings as UTF-16 code units, and thus \U00010000 is actually a two-code-unit string:

>>> list(u'[\U00010000-\U00010001]')
[u'[', u'\ud800', u'\udc00', u'-', u'\ud800', u'\udc01', u']']

The regex parser thus sees the character class containing \udc00-\ud800 which is a "bad character range". In this setting I can't think of a solution other than to match the surrogate pairs explicitly (after ensuring sys.maxunicode == 0xffff):

>>> r = re.compile(u'\ud800[\udc00-\udc01]')
>>> r.match(u'\U00010000')
<_sre.SRE_Match object at 0x10cf6f440>
>>> r.match(u'\U00010001')
<_sre.SRE_Match object at 0x10cf4ed98>
>>> r.match(u'\U00010002')
>>> r.match(u'\U00020000')

这篇关于无法在 Python 2.7 re 中编译 8 位 Unicode 正则表达式范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆