Java 正则表达式

正则表达式

(一)什么是正则表达式?

    符合一定规则的字符串。

(二)常见规则


     A:字符

        x     字符 x。如:'a'表示字符a

        \    反斜线字符。

           新行(换行)符 ('u000A')

           回车符 ('u000D')

     B:字符类

        [abc]          a、b 或 c(简单类)

        [^abc]        任何字符,除了 a、b 或 c(否定)

        [a-zA-Z]     a到z 或 A到Z,两头的字母包括在内(范围)

        [0-9]          0到9的字符都包括

     C:预定义字符类

        .        任何字符。如果就是.字符本身,这样来表示: .

        d      数字:[0-9]

        D     非数字:[^0-9]

        s      空白字符:[ x0Bf ]

        S     非空白字符:[^s]

        w    单词字符:[a-zA-Z_0-9] ,正则表达式里面组成单词的东西必须由这些东西组成
            

      D:边界匹配器

         ^      行的开头

         $      行的结尾

              单词边界,就是不是单词字符的地方。如:java:hello world?

      E:Greedy 数量词

         X?          X,一次或一次也没有

         X*          X,零次或多次

         X+         X,一次或多次

         X{n}      X,恰好 n 次

         X{n,}     X,至少 n 次

         X{n,m}  X,至少 n 次,但是不超过 m 次

(三)常见功能:

     A:判断功能
        String类的public boolean matches(String regex)

            代码案例:

            // String regex ="[1-9][0-9]{4,14}";
            //public boolean matches(String regex)告知此字符串是否匹配给定的正则表达式
            // boolean flag = qq.matches(regex);
            // return flag;           

            return qq.matches("[1-9][0-9]{4,14}");或return qq.matches("[1-9]\d{4,14}");

     B:分割功能
        String类的public String[] split(String regex)

            代码案例:

            String s = "aa.bb.cc";
            String[] strArray = s.split("\.");
            for (int x = 0; x < strArray.length; x++) {
                 System.out.println(strArray[x]);
            }

     C:替换功能
        String类的public String replaceAll(String regex,String replacement)

            代码案例:

            String s = "ab345world67890xyz!";

            // 用*替换掉所有的数字,
           String regex = "\d+";

           String regex = "\d";

           String ss = "*";

           String result = s.replaceAll(regex, ss);

           System.out.println(result);

           // 直接把数字去掉
           String regex = "\d+";

           String ss = "";

           String result = s.replaceAll(regex, ss);

           System.out.println(result);


     D:获取功能
        Pattern和Matcher

         //把正则表达式编译成模式对象
        Pattern p = Pattern.compile("a*b");

        //通过模式对象得到匹配器对象
        Matcher m = p.matcher("aaaaab");

        匹配器对象的功能:

               public boolean find():查找与模式匹配的内容存不存在,当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true

               public String group():返回由以前匹配操作所匹配的输入子序列

               注意:先find() ,然后group()

        代码案例:获取给定字符串中由三个字符组成的单词

              String s = "ni zai zuo shen me?";

              String regex = "\b\w{3}\b";           

              // 把规则编译成模式对象
              Pattern p = Pattern.compile(regex);

              // 通过模式对象得到匹配器对象
              Matcher m = p.matcher(s);

              //通过find()方法查找有没有满足条件的子串

              // public boolean find()
              // boolean flag = m.find();
              // System.out.println(flag);
             

              // 通过group()方法获取find()查找到的子串
              // public String group()
              // String ss = m.group();
              // System.out.println(ss);

              while (m.find()) {
                     System.out.println(m.group());
              }

                    

原文地址:https://www.cnblogs.com/zfsky/p/5951233.html