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'
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'
相关文章推荐
- Python正则表达式使用经典实例
- Java正则表达式匹配使用实例
- java使用正则表达式——实例(转载)
- vs2008中使用正则表达式实例
- [python爬虫] 正则表达式使用技巧及爬取个人博客实例
- js输入框使用正则表达式校验输入内容的实例
- java使用正则表达式——实例
- ruby 正则表达式 使用正负预查
- java正则表达式语法详解及其使用代码实例
- 正则表达式使用实例
- Android 快速使用正则表达式,校验身份证号的实例
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- QT编程之QML使用RegExpValidator(正则表达式)的实例
- python正则表达式使用实例-替换字符串HTML标签
- PowerShell中使用正则表达式筛选数组实例
- java正则表达式语法详解及其使用代码实例
- C++ 调用Boost 使用正则表达式实例
- JQuery选择器中使用正则表达式实例
- Oracle中正则表达式的使用实例教程
- 文本三剑客之使用正则表达式的grep实例