Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式中的Grapheme支持_Python_Regex_Unicode_Grapheme - Fatal编程技术网

python正则表达式中的Grapheme支持

python正则表达式中的Grapheme支持,python,regex,unicode,grapheme,Python,Regex,Unicode,Grapheme,我正在使用awesome模块,尝试其\Xgrapheme支持 首先,我尝试使用普通的 >>> print regex.match('.', 'Ä').group(0) >>> print regex.match('..', 'Ä').group(0) Ä 一切如期进行。转到\X >>> print regex.match('\X', 'Ä').group(0) >>> print regex.match('\X\X',

我正在使用awesome模块,尝试其
\X
grapheme支持

首先,我尝试使用普通的

>>> print regex.match('.', 'Ä').group(0)

>>> print regex.match('..', 'Ä').group(0)
Ä
一切如期进行。转到
\X

>>> print regex.match('\X', 'Ä').group(0)

>>> print regex.match('\X\X', 'Ä').group(0)
Ä
为什么它与
相同?一个
\X
难道不足以捕获a-umlaut吗?是:

  • 我对grapheme或
    \X
    的理解有误吗
  • 我需要先打开某个标志/开关?(我搜索了文档,找不到)
  • 我的环境怎么样?(Python 2.7.3,pip报告regex==2014.12.24)
  • 图书馆里的虫子
  • 还有别的吗

它将
Ä
定义为unicode字符

>>> print regex.match('.', u'Ä').group()
Ä
>>> print regex.match('\X', u'Ä').group()
Ä
Python2和Python3之间的主要区别在于处理文本和字节的基本类型。在Python3上,我们有一个文本类型:
str
,它保存Unicode数据和两个字节类型bytes和bytearray

另一方面,在Python2上,我们有两种文本类型:
str
,无论出于何种目的,它都被限制为ASCII+7位范围以上的一些未定义数据,unicode相当于Python3 str类型和它从Python3继承的一字节类型bytearray


参考-

问题在于默认情况下,python2字符串是字节字符串,这对unicode grapheme没有任何意义。如果您指定使用unicode字符串,它将非常有效

>>> print(regex.match('\X', 'Ä').group(0))

>>> print(regex.match('\X', u'Ä').group(0))
Ä

在python3中,默认字符串是unicode,要指定字节字符串,您应该像这样在前面加上一个
b
b“mybytestring”

我认为问题只存在于2.7版本。在3.4
print(regex.match('\X',Ä').group(0))
中打印字母
Ä