Skip to main content
 首页 » 编程设计

python之为什么 "\p{L}"在此正则表达式中不起作用

2024年08月23日31bonelee

操作系统:Windows 7。Jython 2.7.0“最终版”。

for token in sorted_cased.keys(): 
    freq = sorted_cased[ token ] 
    if freq > 1: 
        print( 'token |%s| unicode? %s' % ( token, isinstance( token, unicode ), ) ) 
        if re.search( ur'\p{L}+', token ): 
            print( '  # cased token |%s| freq %d' % ( token, freq, )) 

sorted_cased 是一个显示标记出现频率的字典。在这里,我试图清除出现频率 > 1 的单词(仅限 unicode 字符)。(注意我使用的是 re.match 而不是 search 但 search 应该在 token)

中检测到事件 1,例如\p{L}

示例输出:

token |Management| unicode? True 
token |n| unicode? True 
token |identifiés| unicode? True 
token |décrites| unicode? True 
token |agissant| unicode? True 
token |tout| unicode? True 
token |sociétés| unicode? True 

没有人认识到其中有一个 [p{L}]。我尝试了各种排列组合:双引号、添加 flags=re.UNICODE 等。

稍后 我被要求解释为什么这不能归类为 How to implement \p{L} in python regex 的副本.它可以,但是......另一个问题的答案并没有引起人们注意使用REGEX MODULE(旧版本?非常新的版本?注意它们是不同的)而不是< strong>RE 模块。为了拯救 future 遇到这个问题的人的毛囊和理智,我请求允许保留当前段落,尽管这个问题被“欺骗”了。

我还尝试安装 Pypi 正则表达式模块 在 JYTHON 下失败(使用 pip)。使用 java.util.regex 可能更好。

请您参考如下方法:

如果您可以访问 Java java.util.regex,最好的选择是使用内置的 \p{L} 类。

Python(包括 Jython 方言)不支持 \p{L} 和其他 Unicode 类别类。也不是 POSIX 字符类。

另一种选择是限制 \w 类,如 (?![\d_])\w 并使用 UNICODE 标志。 If UNICODE is set, this \w will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database. .这种替代方法有一个缺陷:它不能在字符类中使用。

另一个想法是使用 [^\W\d_](带有 re.U 标志),它将匹配任何不是非单词的字符(\W)、数字(\d)和_ 字符。它将有效匹配任何 Unicode 字母。