zoukankan      html  css  js  c++  java
  • 正则入门

    正则表达式必知必会(修订版)

    
    //匹配一组字符串
    
    //[ns]a..xls
    var baseText4 = "na1.xls
    1as.xls
    ca1.xls
    sa1.xls"
    
    //[Rr]eg[Ee]x
    var baseText5 = "The phrase 'regular expression' is often abbreviated ad RegEx or regex"
    
    //[ns]a[0-9].xls
    var baseText6 = "na1.xls
    1as.xls
    ca1.xls
    sa1.xls
    sam.xls"
    
    //#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]
    var baseText7 = "background:#336633;color:#f1f1f1;padding:10px;"
    
    //取非匹配
    //[ns]a[^0-9].xls
    // baseText6
    
    
    //元字符 : 无法用本身代表本身
    // .   [  ]  等
    
    // myArray[[O-9]]
    var baseText8 = " var myArray = new Array()" +
        "for(myArray[0]==0){};  myArray[1]==1"
    
    // \
    var baseText9 = "\home\ben\sss"
    
    //空白元字符
    // []  f 
     
     	 v
    
    // d 数字  D 非数字
    
    // w 字母数字下划线  W 非字母数字下划线
    
    var baseText10 = "11213
    1d23d
    22212
    _ssd123
    abcde
    _2d21d"
    
    // s 空白字符串 S 非空白字符串
    
    //十六进制、 八进制
    //十六进制  x
    //八进制   
    
    //POSIX字符 可移植操作系统接口
    
    
    //
    //重复匹配
    //
    
    // w{2,5}@baidu.com
    var baseText11 = "b@baidu.com 
    ben@baidu.com
    4093@baidu.com"
    
    //  w+@w+.w+
    var baseText12 = 'send personal email to ben@forta.com 
    For questions about a book use support@forta.com 
    Feel free to send unsolicited email to spam@forta.com '
    
    
    //[w.]+@[w.]+.w+
    var baseText13 =  'send personal email to ben@forta.com or ben.forta@firta.com 
    ' +
        'For questions about a book use support@forta.com 
    ' +
        'if you message is ugrent try ben@urgent.forta.com.
    '+
        'Feel free to send unsolicited email to spam@forta.com '
    
    //w+[w.]*@[w.]+.w+
    var baseText14 = "hello .ben@forta.com is my email address"
    
    //http[s]?://[w./]*   or  https?://[w./]*
    var baseText15 = "The URL is http://www.forta.com/ , 
    " +
        "to connect securely use https://www.forta.com/ instead.
    " +
        "https://www.baidu.com/se/"
    
    //匹配的重复次数
    
    //+ * 匹配的字符个数没有上限
    
    // + * ?至少匹配0个或者1个字符
    
    // {} 来设定一个精确地匹配次数的值
    
    
    //  d{1,2}[-/]d{1,2}[-/]d{2,4}  匹配  日期 月份 年份
    var baseText16 = "4/8/03
    10-6-2004
    +2/2/2
    01-01-01"
    
    
    // d+:$d{3,}.d{2}   寻找大于$100的
    var baseText17 = '1001:$496.80 
    1002:$1290.69
    ' +
        '1003:$26.43
    1004:$613.42
    1005:$5.23'
    
    // 贪婪型匹配  *  + {n,} 他们会尽可能从一行文本的头部匹配到末尾
    
    // 懒惰型匹配  *? +? {n,}?   他们会匹配尽可能少的字符
    
    // <[Bb]>.*?</[Bb]> 匹配b标签
    var baseText18 = 'This offer is not available to customers living in <B>ak</B> and <B>HI</B>'
    
    
    //
    //位置匹配
    //
    
    // cat
    var baseText19 = 'The cat scattered his food all over the room'
    // B-B 多个单词边界
    var baseText20 = 'the cat scattered his food all over   -    the room '
    
    // <?xml.*?>
    var baseText21 = '<?xml version="1.0" encoding="UTF-8" ?> 
    ' +
        '<wsdl:definition-src xmlns:wsdl="http://www.w3.org/1999/html">'
    
    // ^s*<?xml.*?>   非空开头查找xml
    var baseText22 = 'this is bad ,real bad 
    ' +
        '<?xml version="1.0" encoding="UTF-8" ?> 
    ' +
        '<wsdl:definition-src xmlns:wsdl="http://www.w3.org/1999/html">'
    
    //
    
    //分行匹配模式
    
    //(?m)^s*//.*$ 查找注释的正则
    
    
    //
    //子表达式
    //
    //
    
    //(d{1,3}.){3}d{1,3}  ip 查找
    var baseText23 = 'Pinging hog.forta.com [12.159.46.200] 
    ' +
        'with 32 bytes of data:'
    
    //(19|20)d{2} 查找4位年份
    var baseText24 = "ID:042 
    " +
        "sex: M
    " +
        "DOB: 1967-08-17
    " +
        "Status: Active"
    
    //一个完整的ip匹配
    //(((d{1,2})|(1d{2})|(2[0-4]d)|(25[0-5])).){3}((d{1,2})|(1d{2})|(2[0-4]d)|(25[0-5]))
    
    
    
    
    //
    //回溯引用: 前后一致匹配
    //
    //回溯允许后面的正则引用前面的匹配结果
    //[ ]+(w+)[ ]+1
    var baseText25 = 'this is a block of text, several words here are are repeated,
    ' +
        ' and and they should not be .'
    //<[hH]([1-6])>.*?</[hH]1>  正确匹配每一个H标签 (不会匹配“<h2></h1>”)
    
    
    
    //回溯例子:  mail替换
    //replace( (w+[w.]*@[w.]+.w+) , <a href="mailto:$1">$1</a> )
    // 替换前
    var baseText26 = "my email is  40935539@qq.com, welcome send to me "
    // 替换后
    // my email is  <a href="mailto:409355439@qq.com">409355439@qq.com</a>, welcome send to me
    
    //替换前
    var baseText27 = "313 - 555 - 1234
    " +
        "248 - 555 - 9999
    " +
        "810 - 555 - 9000"
    // replace( (d{3})( - )(d{3})( - )(d{4})  , ($1) $3 - $5 )
    //替换后
    // (313) 555 - 1234 (248) 555 - 9999 (810) 555 - 9000
    
    
    //
    //前后查找  JS不支持向后查找
    //
    
    //lookahead
    
    
    
    //lookbehind
    var baseText28 = "http://www.forta.com
    " +
        "http://mail.forta.com
    " +
        "ftp://ftp.forta.com"
    
    
    //向前查找 + 向后查找
    //(?<=([hH][1-9]>)).*(?=</[hH][1-9]>)
    
    //example:      " <h3>REGEXP</h3>"
    
    //取非
    //正向前查找(?=)  负向前查找(?!)
    //正向前查找(?<=)  负向后查找(?<!)
    
    //(?<!$)d+ 查找不含$的数字
    var baseText29 = "i paid $30 for 100 apples, 50 organges ,and 60 pears . i saved $5 on this order ."
    
    
  • 相关阅读:
    窗口显示于parent控件上
    DELPHI SOCKET 通信编程要点小结
    dxBarManagerToDxNavBar方法
    DLL直接返回对象
    海量数据库的查询优化及分页算法方案
    excel怎么只打印某页?excel怎么只打印某几页
    HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析
    excel中如何设置只打印第一页
    Navicat Premium 常用功能讲解
    laravel查询构造器DB还是ORM,这两者有什么区别,各该用在什么场景中
  • 原文地址:https://www.cnblogs.com/iyueyao/p/4476514.html
Copyright © 2011-2022 走看看