zoukankan      html  css  js  c++  java
  • ruby使用正则表达式之实例1

    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'

  • 相关阅读:
    栈和队列
    链表
    map
    二叉平衡树旋转
    二叉排序树详情
    红黑树详情
    查并集
    动态规划
    位操作
    字典树
  • 原文地址:https://www.cnblogs.com/yinzhenyuan/p/ruby_regular_expression.html
Copyright © 2011-2022 走看看