Ruby 匹配中文的正则表达片段

ruohanc · September 21, 2012 · Last by victorialice replied at March 19, 2016 · 18197 hits

今天在 twitter 上瞅见的:

"这段正则 /[一 - 龠]+/ 能匹配简体和繁体,至少 Objective-C、JavaScript 和 Ruby 都验证过了,就是不知道字数范围有多少……" -- ‏@chrisyipw 推文

ruby-1.9.3-p194

("一".."龥").to_a.size
=> 20902

这个说法是有问题的吧。看 Unicode 文档,CJK 是分成好几段的

http://www.unicode.org/versions/Unicode6.0.0/ch12.pdf

0x3400 ~ 0x4DBF
0x4E00 ~ 0x9FFF
0xF900 ~ 0xFAFF
0x20000 ~ 0x2A6DF
0x2A700 ~ 0x2B73F
0x2B740 ~ 0x2B81F
0x2F800 ~ 0x2FA1F

一 - 龠 也就 4E00 ~ 9FA0 龥 也就 9FA5

原来匹配简体和繁体的正则是这样的

#5 楼 @luikore 这个神奇!哪里看到的.. :thumbsup:

汗,那条推并不是完整的,完整的在这:http://chrisyip.im/post/regular-expression-for-cjk/

Ruby 和部分语言可以直接 #{Han} 等方式匹配特定的语言,但是对于某些语言,如 JavaScript,是不可能如此简便的,我发那条推和写那篇文的目的是针对我会用到的语言。

#7 楼 @luikore 请教下,我的输入字符编码是 utf8,有"12321313"和"下载"这两种字符,在我的系统上进行匹配的时候 使用/\p{Han}+/u,匹配不了,使用/[\u4e00-\u9fa5]/可以匹配,但把两种字符都匹配了,这种是否区分不了。。 if hash["serv_crc"] =~ /[\u4e00-\u9fa5]/ line[3] = crc32(hash["serv_crc"]) ATT::KeyLog::debug "serv_crc:#{hash["serv_crc"]} convert to crc 3:#{line[3]}" end

#9 楼 @praguepp 你的 ruby 版本是 1.8 吧... 要么升级 1.9/2.0, 要么用这个:

/(
    \xe4[\xb8-\xbf][\x80-\xbf]
    |[\xe5-\xe8][\x80-\xbf][\x80-\xbf]
    |\xe9[\x80-\xbd][\x80-\xbf]
    |\xe9\xbe[\x80-\xa5]
)+/x

多年前写的可用在 1.8 的针对各种编码的正则:https://gist.github.com/luikore/149493

#10 楼 @luikore 你真厉害,得多向你学习。 是 1.87。 试了下,提示无效的正则表达式 test_create_mem_u_log(TestAreaCenterClientOperation): ATT::Exceptions::LoadError: loading C:/operator/keywords/helper/area_center/area_center_client_operation.rb error: C:/operator/keywords/helper/area_center/area_center_client_operation.rb:231: invalid regular expression: /(\xe4[xb8-\xbf][\x80-\xbf]|[\xe5-xe8][\x80-\xbf][\x80-\xbf]|\xe9[\x80-\xbd][\x80-\xbf]|\xe9\xbe[\x80-\xa5])+/ C:/operator/keywords/helper/area_center/area_center_client_operation.rb:237: invalid regular expression: /(\xe4[xb8-\xbf][\x80-\xbf]|[\xe5-xe8][\x80-\xbf][\x80-\xbf]|\xe9[\x80-\xbd][\x80-\xbf]|\xe9\xb e[\x80-\xa5])+/ C:/ATT_rake_server_ruby187/ruby/lib/ruby/gems/1.8/gems/att-1.1.0/lib/att/load_keyword.rb:76:in `require_file'

#11 楼 @praguepp 你把 [\xe5-\xe8] 写成 [\xe5-xe8] 了... 少了个反斜线...

#12 楼 @luikore 是我不够细心,下次会注意。 现在可以了,谢谢

#5 楼 @luikore

/\p{Han}+/u 中的 /u 是什么意思呢?看了半天文档还是没懂。 😄

#14 楼 @xiaoronglv 让这个正则的编码是 utf-8 的意思

/a/.encoding   # US-ASCII
/a/u.encoding # UTF-8
wikimo in 分享下 ruby 匹配 @ 艾特用户名的方法 mention this topic. 10 Sep 23:01
You need to Sign in before reply, if you don't have an account, please Sign up first.