正则表达式的 Python 和 Unicode 块 [英] Python and Unicode Blocks for regex

查看:35
本文介绍了正则表达式的 Python 和 Unicode 块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Perl 领域,我可以执行以下操作来测试特定 unicode 块中字符串的成员资格:

Coming from the land of Perl, I can do something like the following to test the membership of a string in a particular unicode block:

# test if string has any katakana script characters
my $japanese = "カタカナ";
if ($japanese =~ /\p{InKatakana}/) {
   print "string has katakana"
}

我读到 Python 不支持 unicode 块(真的吗?) - 那么手动实现这一点的最佳方法是什么?例如,上述 {InKatakana} 的 unicode 块范围应该是 U+30A0…U+30FF.如何在 Python 中测试 unicode 范围?还有其他推荐的解决方案吗?

I've read that Python does not support unicode blocks (true?) - so what's the best way to impliment this manually? For example, the above unicode block range for {InKatakana} should be U+30A0…U+30FF. How can I test the unicode range in Python? Any other recommended solutions?

我不想使用像 Ponyguruma 这样的外部包装器来限制推出/维护的依赖项数量.

I would prefer not to go with an external wrapper like Ponyguruma to limit the number of dependencies for roll-out/maintenance.

推荐答案

>>> re.search(u'[\u30a0-\u30ff]', u'カタカナ')
<_sre.SRE_Match object at 0x7fa0dbb62578>

这篇关于正则表达式的 Python 和 Unicode 块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆