在Python中匹配Unicode字边界

时间:2016-08-24 20:45:30

标签: python regex python-3.x unicode

为了匹配Python中的Annex #29]中定义的Unicode字边界,我一直在使用带有regex标记的regex.WORD | regex.V1包(regex.UNICODE应该因为模式是一个Unicode字符串,所以默认为以下列方式:

>>> s="here are some words"
>>> regex.findall(r'\w(?:\B\S)*', s, flags = regex.V1 | regex.WORD)
['here', 'are', 'some', 'words']

在这个相当简单的案例中,它运作良好。但是,我想知道输入字符串包含某些标点符号时的预期行为是什么。在我看来,WB7说,例如x'z中的撇号不符合词边界,这似乎确实如此:

>>> regex.findall(r'\w(?:\B\S)*', "x'z", flags = regex.V1 | regex.WORD)
["x'z"]

但是,如果有元音,情况会发生变化:

>>> regex.findall(r'\w(?:\B\S)*', "l'avion", flags = regex.V1 | regex.WORD)
["l'", 'avion']

这表明正则表达式模块实现了 Notes 部分标准中提到的规则WB5a。但是,此规则还表示行为应该与\u2019(右单引号)相同,我无法重现:

>>> regex.findall(r'\w(?:\B\S)*', "l\u2019avion", flags = regex.V1 | regex.WORD)
['l’avion']

此外,即使使用“普通”撇号,连字(或y)似乎表现为“非元音”:

>>> regex.findall(r'\w(?:\B\S)*', "l'œil", flags = regex.V1 | regex.WORD)
["l'œil"]
>>> regex.findall(r'\w(?:\B\S)*', "J'y suis", flags = regex.V1 | regex.WORD)
["J'y", 'suis']

这是预期的行为吗? (以上所有示例均使用regex 2.4.106和Python 3.5.2执行)

1 个答案:

答案 0 :(得分:6)

1- 正确的单个引号 似乎只是在source file中错过了:

/* Break between apostrophe and vowels (French, Italian). */
/* WB5a */
if (pos_m1 >= 0 && char_at(state->text, pos_m1) == '\'' &&
  is_unicode_vowel(char_at(state->text, text_pos)))
    return TRUE;

2- Unicode元音由is_unicode_vowel()函数确定,该函数转换为此列表:

a, à, á, â, e, è, é, ê, i, ì, í, î, o, ò, ó, ô, u, ù, ú, û

因此LATIN SMALL LIGATURE OE œ字符不被视为unicode元音:

Py_LOCAL_INLINE(BOOL) is_unicode_vowel(Py_UCS4 ch) {
#if PY_VERSION_HEX >= 0x03030000
    switch (Py_UNICODE_TOLOWER(ch)) {
#else
    switch (Py_UNICODE_TOLOWER((Py_UNICODE)ch)) {
#endif
    case 'a': case 0xE0: case 0xE1: case 0xE2:
    case 'e': case 0xE8: case 0xE9: case 0xEA:
    case 'i': case 0xEC: case 0xED: case 0xEE:
    case 'o': case 0xF2: case 0xF3: case 0xF4:
    case 'u': case 0xF9: case 0xFA: case 0xFB:
        return TRUE;
    default:
        return FALSE;
    }
}

此错误现已在bug report后的regex 2016.08.27中修复。 [_regex.c:#1668]