Python and Unicode Blocks for regex

2020-04-14 04:39发布

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"
}

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?

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

2条回答
放我归山
2楼-- · 2020-04-14 05:07

As Ignacio said, the re expression is very useful. Don't forget the import first. This search only finds full-width katakana.

import re  
re.search(u'[\u30a0-\u30ff]', u'カタカナ')  

Or you might already have a string on hand.

import re  
x = "カタカナ"  
re.search(u'[\u30a0-\u30ff]', x.decode('utf-8'))
查看更多
够拽才男人
3楼-- · 2020-04-14 05:10
>>> re.search(u'[\u30a0-\u30ff]', u'カタカナ')
<_sre.SRE_Match object at 0x7fa0dbb62578>
查看更多
登录 后发表回答