Ruby语言正则表达式(regular expression )问题

『》』Ruby语言正则表达式(regular expression )问题
『《』中文无法搜索,如下二组代码显示不同的结果:
str = "上海是我自己的故乡。绍兴是我父亲的故乡,也是我的。。"
str =~ /(我自己)(我父亲)/
$~.to_a.each do |item|
p item # => nil
end

"foobarbaz" =~ /(foo)(bar)/
p $~[0..20] # => ["foobar", "foo", "bar"]
。。。我以为,这是一个错误。在Eclipse插件QuickREx也是不行。
『《』在Eclipse插件QuickREx使用正则表达式和Ruby语言使用不同:
str = "上海是我自己的故乡,绍兴是我父亲的故乡,也是我的。。"
str =~ /我自己|我父亲/
$~.to_a.each do |item|
puts item # => ["我自己"] ruby
# => ["我自己", "我父亲"] eclipse QuickREx
end

/foo|bar/ =~ "foobarbaz"
p $~[0..20] # => ["foo"] ruby
# => ["foo", "bar"] eclipse QuickREx
。。。

Comments

Popular posts from this blog

英语字母a的发音