孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

ruby语言中用~/字符/来匹配表达式,$`得到匹配之前的那部分字符串,$'得到匹配之后的字符串,$&得到匹配到的字符串,如下所示

def show_regexp(a,re)
    if a=~re
        puts "#{$`}<<#{$&}>>#{$'}"
    else
        puts "no match"
    end
end

show_regexp('very interesting',/t/)
show_regexp('Fats Waller',/a/)
show_regexp('Fats Waller',/l/)
show_regexp('Fats Waller',/z/)

posted on 2012-03-23 20:09  孤独的猫  阅读(272)  评论(0编辑  收藏  举报