我只是在jruby上运行时发现的。当文本中有中文/日文时,MatchData.begin() 将始终返回 0。
我使用以下 ruby 脚本进行了测试:
# encoding: utf-8
text = 'i love dog @chichi dog dog'
text_cn = '我爱你狗狗 @chichi 狗狗'
text_jp = '私はあなたの犬を愛して @chichi ドッグ'
reg = /([@@])([a-zA-Z0-9_]{1,20})(.*)/o
puts "==== Text in English ===="
text.scan(reg) do |before, at, screen_name, list_slug|
puts $~.inspect
puts "1. #{$~.begin(1)}"
puts "2. #{$~.begin(2)}"
puts "3. #{$~.begin(3)}"
end
puts "==== Text in Chinese ===="
text_cn.scan(reg) do |before, at, screen_name, list_slug|
puts $~.inspect
puts "1. #{$~.begin(1)}"
puts "2. #{$~.begin(2)}"
puts "3. #{$~.begin(3)}"
end
puts "==== Text in Japanese ===="
text_jp.scan(reg) do |before, at, screen_name, list_slug|
puts $~.inspect
puts "1. #{$~.begin(1)}"
puts "2. #{$~.begin(2)}"
puts "3. #{$~.begin(3)}"
end
结果是jruby-1.6.7.2
:
$ jruby -S test.rb
==== Text in English ====
#<MatchData "@chichi dog dog" 1:"@" 2:"chichi" 3:" dog dog">
1. 11
2. 12
3. 18
==== Text in Chinese ====
#<MatchData "@chichi 狗狗" 1:"@" 2:"chichi" 3:" 狗狗">
1. 0
2. 0
3. 0
==== Text in Japanese ====
#<MatchData "@chichi ドッグ" 1:"@" 2:"chichi" 3:" ドッグ">
1. 0
2. 0
3. 0
如果我在ruby-1.9.2-p290
一切正确的情况下运行它,我不确定是否需要对我错过的 jruby 进行一些设置?
有任何想法吗?