zoukankan      html  css  js  c++  java
  • Java 正则表达式

     

    正则表达式:regex

    \    反斜线
    .    任意字符
    d    数字0-9
    D 非数字: [^0-9]
    
    [abc] a、b 或 c(简单类) 
    [^abc] 任何字符,除了 a、b 或 c(否定) 
    [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围) 
    [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集) 
    [a-z&&[def]] d、e 或 f(交集) 
    [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
    [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
    
    ^    行的开头 
    $    行的结尾 
        单词边界 
    B    非单词边界 
    
    X?    X,一次或一次也没有 
    X*    X,零次或多次 
    X+    X,一次或多次 
    X{n}    X,恰好 n 次 
    X{n,}    X,至少 n 次 
    X{n,m}    X,至少 n 次,但是不超过 m 次

    正则表达式的常用:

    1 匹配:
      字符串提供的方法
      boolean matches(String regex)
      手机号:1[34578][0-9]{9}
    2 切割:
      字符串提供的方法
      String[] split(String regex)
    3 替换:
      字符串提供的方法
      String replaceAll(Strinf regex, String replacement):
      把字符串中符合regex规则的替换为replacement
      使用$可以取得正则表达式中第几组的值
        eg:
        String ss = "zhangsan$$$lisi^^^^^^^^^jksjda####";
        String regex = "(.)\1+";
    
        String s1 = ss.replaceAll(regex, "$1");//取regex第一组的值
        Systm.out.println(s1);
    4 获取:
      把正则表达式编译成Pattern对象
      Pattern不具备获取的功能,需要得到一个Matcher对象
      Matcher具备获取的功能
    
      Pattern对象:
        Pattern.compile(regex):
        将正则表达式编译成Pattern对象
    
        eg:
        String str = "zhu yi le wo shi hen kai xin de ";
        String regex = "[a-z]{3}":
    
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);



  • 相关阅读:
    Web自动化测试之滑动验证码的解决方案
    设计模式之单例
    设计模式之原型
    设计模式之建造者
    DG环境RMAN删除归档报错RMAN-08137: archived log not deleted【转载】
    RFS[3]: No standby redo logfiles available for thread 1
    spring-boot之webflux简单入门
    spring-boot-security组件总结补充
    spring-boot启用security组件 · 下
    spring-boot启用security组件 · 中
  • 原文地址:https://www.cnblogs.com/roxy/p/7341696.html
Copyright © 2011-2022 走看看