您的位置:首页 > 编程语言 > Ruby

ruby使用正则表达式之实例1

2011-11-01 10:22 316 查看
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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: