aa="http://www.dangdang.com123"

bb=aa[/\d+/] #匹配字符串中的数字
puts bb #should be output '123'

 

regexp=/\d+/

puts aa.gsub(regexp,' ') #匹配字符串中数字以外的字符

cc=aa[/\d{2}/] #匹配字符串中的2个数字
puts cc #should be output '12'

dd=aa[/3$/] #判断字符串是否以3结尾
puts dd #should be output '3'

ee=aa[/^https/] #判断字符串是否以https开头
puts ee #should be output 'nil'
if ee == nil
  puts "right:string is not begin from https"
end
 
ff=aa[/:\/\//] #正则中匹配/时,前面需要加转义\
puts ff #should be output '://'

puts gg=aa.split('://')[1][/\D+/] #匹配://后面的字符串
#should be output 'www.dangdang.com'

posted on 2011-11-01 10:22  AdaYin  阅读(309)  评论(0编辑  收藏  举报