在Python中,如何列出POSIX扩展正则表达式`[:space:]`匹配的所有字符? [英] In Python, how to list all characters matched by POSIX extended regex `[:space:]`?

查看:42
本文介绍了在Python中,如何列出POSIX扩展正则表达式`[:space:]`匹配的所有字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何列出POSIX扩展正则表达式[:space:]匹配的所有字符?

是否有一种程序化的方式来提取 [:space:] 覆盖的 Unicode 代码点?

解决方案

使用生成器代替列表推导式,使用 xrange 代替 range:

<预><代码>>>>s = u''.join(unichr(c) for c in xrange(0x10ffff+1))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.文件<stdin>",第 1 行,在 <genexpr> 中.ValueError: unichr() arg 不在范围内 (0x10000)(窄 Python 构建)

哎呀:一般使用sys.maxunicode.

<预><代码>>>>s = u''.join(unichr(c) for c in xrange(sys.maxunicode+1))>>>进口重新>>>re.findall(r'\s', s)[u'\t', u'\n', u'\x0b', u'\x0c', u'\r', u' ']

哎呀:嗯,不间断空间"怎么样?等?

<预><代码>>>>re.findall(r'\s', s, re.UNICODE)[u'\t', u'\n', u'\x0b', u'\x0c', u'\r', u'\x1c', u'\x1d', u'\x1e', u'\x1f', 你' ', u'\x85', u'\xa0', u'\u1680', u'\u180e', u'\u2000', u'\u2001', u'\u2002', u'\u2003'、u'\u2004'、u'\u2005'、u'\u2006'、u'\u2007'、u'\u2008'、u'\u2009'、u'\u200a', u'\u2028', u'\u2029', u'\u202f', u'\u205f', u'\u3000']

那些东西是什么?unicodedata.name 是你的朋友:

<预><代码>>>>从 unicodedata 导入名称>>>对于 c in re.findall(r'\s', s, re.UNICODE):... 打印 repr(c), name(c, '')...你'\t'联合国'你'\x0b'你'\x0c'你'\r'你'\x1c'你'\x1d'你'\x1e'你'\x1f'你的空间你'\x85'u'\xa0' 无间断空间u'\u1680' 奥格姆空间标记u'\u180e' 蒙古元音分隔符u'\u2000' EN QUADu'\u2001' EM QUADu'\u2002' EN 空间u'\u2003' 电磁空间u'\u2004' 每个 EM 三个空间u'\u2005' 4-PER-EM 空间u'\u2006' 每个 EM 六个空间u'\u2007' 图形空间u'\u2008' 标点空格u'\u2009' 薄空间u'\u200a' 头发空间u'\u2028' 行分隔符u'\u2029' 段落分隔符u'\u202f' 狭窄的不间断空间u'\u205f' 中等数学空间u'\u3000' 表意空间

In Python, how to list all characters matched by POSIX extended regex [:space:]?

Is there a programmatic way of extracting the Unicode code points covered by [:space:]?

解决方案

Using a generator instead of a list comprehension, and xrange instead of range:

>>> s = u''.join(unichr(c) for c in xrange(0x10ffff+1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <genexpr>
ValueError: unichr() arg not in range(0x10000) (narrow Python build)

Whoops: in general use sys.maxunicode.

>>> s = u''.join(unichr(c) for c in xrange(sys.maxunicode+1))
>>> import re
>>> re.findall(r'\s', s)
[u'\t', u'\n', u'\x0b', u'\x0c', u'\r', u' ']

Whoops: Ummm what about "no-break space" etc?

>>> re.findall(r'\s', s, re.UNICODE)
[u'\t', u'\n', u'\x0b', u'\x0c', u'\r', u'\x1c', u'\x1d', u'\x1e', u'\x1f', u' '
, u'\x85', u'\xa0', u'\u1680', u'\u180e', u'\u2000', u'\u2001', u'\u2002', u'\u2
003', u'\u2004', u'\u2005', u'\u2006', u'\u2007', u'\u2008', u'\u2009', u'\u200a
', u'\u2028', u'\u2029', u'\u202f', u'\u205f', u'\u3000']

What is all that stuff? unicodedata.name is your friend:

>>> from unicodedata import name
>>> for c in re.findall(r'\s', s, re.UNICODE):
...     print repr(c), name(c, '')
...
u'\t'
u'\n'
u'\x0b'
u'\x0c'
u'\r'
u'\x1c'
u'\x1d'
u'\x1e'
u'\x1f'
u' ' SPACE
u'\x85'
u'\xa0' NO-BREAK SPACE
u'\u1680' OGHAM SPACE MARK
u'\u180e' MONGOLIAN VOWEL SEPARATOR
u'\u2000' EN QUAD
u'\u2001' EM QUAD
u'\u2002' EN SPACE
u'\u2003' EM SPACE
u'\u2004' THREE-PER-EM SPACE
u'\u2005' FOUR-PER-EM SPACE
u'\u2006' SIX-PER-EM SPACE
u'\u2007' FIGURE SPACE
u'\u2008' PUNCTUATION SPACE
u'\u2009' THIN SPACE
u'\u200a' HAIR SPACE
u'\u2028' LINE SEPARATOR
u'\u2029' PARAGRAPH SEPARATOR
u'\u202f' NARROW NO-BREAK SPACE
u'\u205f' MEDIUM MATHEMATICAL SPACE
u'\u3000' IDEOGRAPHIC SPACE

这篇关于在Python中,如何列出POSIX扩展正则表达式`[:space:]`匹配的所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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