Nik*_*Nik 3 python regex unicode python-3.x python-re
我正在尝试编写一个与 Python 3 中的 Unicode 小写字符匹配的正则表达式。我正在使用该re库。例如,re.findall(some_pattern, 'u\xe2\x88\x8f\xc3\xb1K\xce\xb8') 应该返回['u', '\xc3\xb1', '\xce\xb8'].
在 Sublime Text 中,我只需键入即可[[:lower:]]找到这些字符。
我知道 Python 可以使用 匹配任何 Unicode 字符re.compile('[^\\W\\d_]'),但我特别需要区分大写和小写。我也知道它re.compile('[a-z]')会匹配任何 ASCII 小写字符,但我的数据是 UTF-8,并且它包含许多非 ASCII 字符\xe2\x80\x94我检查过。
Python 3 中的正则表达式可以实现这一点吗?还是我需要采取替代方法?我知道其他方法可以做到这一点。我只是希望使用正则表达式。
\n您可以使用支持 POSIX 字符类的正则表达式模块:
\nimport regex \n\n>>> regex.findall(\'[[:lower:]]\', \'u\xe2\x88\x8f\xc3\xb1K\xce\xb8\')\n[\'u\', \'\xc3\xb1\', \'\xce\xb8\']\nRun Code Online (Sandbox Code Playgroud)\n或者,使用或的Unicode Category Class:\\p{Ll}\\p{Lowercase_Letter}
>>> regex.findall(r\'\\p{Ll}\', \'u\xe2\x88\x8f\xc3\xb1K\xce\xb8\')\n[\'u\', \'\xc3\xb1\', \'\xce\xb8\']\nRun Code Online (Sandbox Code Playgroud)\n或者只使用 Python 的字符串逻辑:
\n>>> [c for c in \'u\xe2\x88\x8f\xc3\xb1K\xce\xb8\' if c.islower()]\n[\'u\', \'\xc3\xb1\', \'\xce\xb8\']\nRun Code Online (Sandbox Code Playgroud)\n无论哪种情况,都要小心这样的字符串:
\n>>> s2=\'\\u0061\\u0300\\u00E0\'\n>>> s2\n\'\xc3\xa0\xc3\xa0\'\nRun Code Online (Sandbox Code Playgroud)\n第一个字素 是与 的组合字符\'\xc3\xa0\'的结果,其中第二个字素是该特定代码点的结果。如果您在此处使用字符类,它将匹配而不是组合重音:\'a\'\'\xcc\x80\'\'\xc3\xa0\'\'a\'
>>> regex.findall(\'[[:lower:]]\', s2)\n[\'a\', \'\xc3\xa0\']\n>>> [c for c in s2 if c.islower()]\n[\'a\', \'\xc3\xa0\']\nRun Code Online (Sandbox Code Playgroud)\n要解决这个问题,您需要用更复杂的正则表达式模式来解决这个问题或规范化字符串:
\n>>> regex.findall(\'[[:lower:]]\', unicodedata.normalize(\'NFC\',s2))\n[\'\xc3\xa0\', \'\xc3\xa0\']\nRun Code Online (Sandbox Code Playgroud)\n或按字素循环:
\n>>> [c for c in regex.findall(r\'\\X\', s2) if c.islower()]\n[\'\xc3\xa0\', \'\xc3\xa0\']\nRun Code Online (Sandbox Code Playgroud)\n