javaRegex總結

來源:互聯網
上載者:User

標籤:Regex

package pack;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo {    public static void main(String[] args) {        //method1();//匹配        //method2();//切割        //method3();//替換        /*前面只是用了String的方法*/        method4();    }    public static void method1() {        String qq = "1881265";        String regex1 = "[1-9]\\d{4,14}";        sys(qq.matches(regex1));  //是否符合regex規則        String s = "e";        //String regex = "[a-zA-Z]";  // 字母        //String regex = "[a-d[m-p]]";   //a到d 或 m到p        /* \d     數字[0-9]         * \D     非數字[^0-9]         * \s     空白字元         * \S     非空白字元         * \w     單詞字元[a-zA-Z0-9_]         * \W     非單詞字元         *          *          *          * X?      一次或零次         * X*      零次或多次         * X+      一次或多次         * X{n}    恰好n次         * X{n,}   至少n次         * X{n,m}  n次到m次         *           */        String regex = "\\d";            sys(s.matches(regex));    }    public static void method2() {        /*String s = "zhang   li    san";        String regex = "\\s+";        String[] arr = s.split(regex);        for(String ss : arr)            sys(ss);*/        /*        String s = "zhang.li.san";        String regex = "\\.";  //  \.代表Regex中的.即任一字元,\\.代表普通點,                                                                                                           不能直接用.切,它是特殊字元        String[] arr = s.split(regex);        for(String ss : arr)            sys(ss);*/        /*String s = "www\\abs\\1.txt";        String regex = "\\\\";          String[] arr = s.split(regex);        for(String ss : arr)            sys(ss);*/        String s = "wekkyduffopxxxs";        //按疊詞切/*按照疊詞完成切割,為了讓規則是結果被重用,可以將規則封裝成一個組,用()完成,組都有編號,從1開始*/        String regex = "(.)\\1+"; //括弧是組的概念,\1是再次出現,\1+是一次或多次        String[] arr = s.split(regex);        for(String ss : arr)            sys(ss);    }    public static void method3() {        String s = "wj3374zns399nvn9999klf3333";  //將數字替換為#號        String regex = "[0-9]";        s.replaceAll(regex, "#");        sys(s);        /*String s = "wekkyduffopxxxs";  //將疊詞替換為單個,如:zzzz->z        String regex = "(.)\\1+";        s.replaceAll(regex, "$1");        sys(s);*/    }    public static void method4() {        String s = "zhas asb cjcdj csj cbbdv";        String regex = "[a-z]{3}";        //將規則封裝成對象        Pattern p = Pattern.compile(regex);        //擷取匹配器對象        Matcher m = p.matcher(s);        sys(m.matches()); //匹配全部,為false,注意:指標向後移了,會影響下面的結果        while(m.find()) {            sys(m.group());  //匹配到的字串            sys(m.start()+"..."+m.end()); //匹配到的字串起止位        }    }    public static void sys(Object obj) {        System.out.println(obj);    }}

javaRegex總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.