【发布时间】:2015-12-06 12:04:49
【问题描述】:
操作系统: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 应该检测到事件1,例如\p{L} token)
样本输出:
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 (旧版本?非常新版本?注意它们是不同的)而不是 RE 模块。为了保存毛囊和未来遇到这个问题的人的理智,我要求允许保留本段,尽管问题是“欺骗”。
我也尝试安装 Pypi 正则表达式模块 在 JYTHON 下失败(使用 pip)。使用 java.util.regex 可能更好。
【问题讨论】:
-
Python re 模块不支持
\p{L}速记Unicode类别类。 -
使用
regex模块.. -
谢谢你们!我很困惑,因为这里确实有一个 python 问题stackoverflow.com/questions/17595979/… 使用 \p{L} ...而且,是的,regex(我从未听说过的模块!)
-
另一种选择是限制
\w类,如(?![\d_])\w并使用 re.UNICODE 标志。 If UNICODE is set, this\wwill match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database. -
@stribizhev 谢谢你。我现在已经安装了正则表达式模块。但我不确定它是旧的还是新的……新的似乎非常非常新:pypi.python.org/pypi/regex。我提到的另一个问题大概是使用旧问题......
标签: python regex unicode jython