正则表达式中的基本正则规则详解01

转载原文章链接:http://www.jb51.cc/article/p-mumenqay-bqm.html

字符组

  • 正则表达式的最基本结构之一。
  • 作用:规定某个位置能够出现的字符。
  • 形式:以”[…]”给出,在方括号内列出字符。或者简写字符。
  • 方括号中的字符为某个位置是否出现的字符,例如[123],如果当前位置出现1或者2或者3的话,它都能匹配,但是只能出现一个数字。

例子:
判断十进制字符。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li1 {
    public static void main(String[] args) {
         String string="4";
         String rex="[0123456789]";

         Pattern pattern=Pattern.compile(rex);
         Matcher matcher=pattern.matcher(string);

         System.out.println("/*********本例子用于判断数字是不是十进制*********/");

         if(matcher.matches()){
             System.out.println("\t\t是十进制数");
         }else{
             System.out.println("\t\t不是十进制数");
         }
    }
}

运行结果:

/*********本例子用于判断数字是不是十进制*********/
        是十进制数

连字符

这个是连字符 “-”
但是上面的例子用来表示十进制[0123456789]太过累赘,所以可以使用连字符用来简化,但是连字符只能够简化是相连接,中间没有空缺的。即可以这样表示[0-9]就行了。也可以这样表示[0-789]这样也是可以的,想要表达的意思就是用了连字符只是起到简化的作用。

可以测试下:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li1 {
    public static void main(String[] args) {
         String string="4";
         String rex="[0-9]";

         Pattern pattern=Pattern.compile(rex);
         Matcher matcher=pattern.matcher(string);

         System.out.println("/*********本例子用于判断数字是不是十进制*********/");

         if(matcher.matches()){
             System.out.println("\t\t是十进制数");
         }else{
             System.out.println("\t\t不是十进制数");
         }
    }
}

运行结果:

/*********本例子用于判断数字是不是十进制*********/
        是十进制数

注意事项

  • 在字符组的内部,只有当连字符出现在两个字符之间时,才能表示字符的范围;如果出现在字符组的开头或者结尾,则只能够表示单个字符’-‘即连字符本身这个字符。

排除型字符组

  • 作用:规定某个位置不容许出现的字符。
  • 形式:以”[^…]”给出,在方括号内列出不容许出现的字符。
  • 排除型字符组仍然必须匹配一个字符。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li2 {
         public static void main(String[] args) {
            String [] strs={"1","2","a","A","\n","\t","\r"," ","哈"};
            String rex="[^123]";

            Pattern pattern=Pattern.compile(rex);

            for(String str:strs){
                Matcher matcher=pattern.matcher(str);
                System.out.println("字符串:"+str+"匹配结果"+"["+matcher.matches()+"]");
            }
        }
}

运行结果:

字符串:[1]匹配结果[false]
字符串:[2]匹配结果[false]
字符串:[a]匹配结果[true]
字符串:[A]匹配结果[true]
字符串:[
]匹配结果[true]
字符串:[   ]匹配结果[true]
字符串:[
]匹配结果[true]
字符串:[ ]匹配结果[true]
字符串:[哈]匹配结果[true]

所以排除型表示的意思就是排除当前的字符,然后满足其他的所有字符。

字符组简记法

  • 对于常用的字符组,正则表达式提供了相应的简记法,方便地表示它们。
  • \d 相当于 [0-9]
  • \D 相当于 [^0-9]
  • \w 相当于 [0-9a-zA-Z_]
  • \W相当于 [^0-9a-zA-Z_]
  • \s 匹配空白字符(回车\r、换行\n、制表、空格)
  • \S 匹配非空白字符
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li3 {
          public static void main(String[] args) {
            String rex1="\\d";
            String rex2="\\D";
            String rex3="\\w";
            String rex4="\\W";
            String rex5="\\s";
            String rex6="\\S";

            String [] string=new String[]{"1","B","b","呵","!","!"};

            System.out.println("/*************\\d****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex1);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"["+str+"]"+"匹配["+rex1+"]");
                }else{
                    System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex1+"]");
                }
            }

            System.out.println("/*************\\D****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex2);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"["+str+"]"+"匹配["+rex2+"]");
                }else{
                    System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex2+"]");
                }
            }

            System.out.println("/*************\\w****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex3);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"["+str+"]"+"匹配["+rex3+"]");
                }else{
                    System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex3+"]");
                }
            }

                System.out.println("/*************\\W****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex4);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"["+str+"]"+"匹配["+rex4+"]");
                }else{
                    System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex4+"]");
                }
            }

                    System.out.println("/*************\\s****************/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex5);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"["+str+"]"+"匹配["+rex5+"]");
                        }else{
                            System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex5+"]");
                        }
                    }

                    System.out.println("/*************\\S****************/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex6);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"["+str+"]"+"匹配["+rex6+"]");
                        }else{
                            System.out.println("字符串:"+"["+str+"]"+"不匹配["+rex6+"]");
                        }
                    }


        }
}

运行结果:

/*************\d****************/
字符串:[1]匹配[\d]
字符串:[2]匹配[\d]
字符串:[A]不匹配[\d]
字符串:[B]不匹配[\d]
字符串:[a]不匹配[\d]
字符串:[b]不匹配[\d]
字符串:[
]不匹配[\d]
字符串:[   ]不匹配[\d]
字符串:[
]不匹配[\d]
字符串:[ ]不匹配[\d]
字符串:[]不匹配[\d]
字符串:[!]不匹配[\d]
字符串:[]不匹配[\d]
/*************\D****************/
字符串:[1]不匹配[\D]
字符串:[2]不匹配[\D]
字符串:[A]匹配[\D]
字符串:[B]匹配[\D]
字符串:[a]匹配[\D]
字符串:[b]匹配[\D]
字符串:[
]匹配[\D]
字符串:[   ]匹配[\D]
字符串:[
]匹配[\D]
字符串:[ ]匹配[\D]
字符串:[]匹配[\D]
字符串:[!]匹配[\D]
字符串:[]匹配[\D]
/*************\w****************/
字符串:[1]匹配[\w]
字符串:[2]匹配[\w]
字符串:[A]匹配[\w]
字符串:[B]匹配[\w]
字符串:[a]匹配[\w]
字符串:[b]匹配[\w]
字符串:[
]不匹配[\w]
字符串:[   ]不匹配[\w]
字符串:[
]不匹配[\w]
字符串:[ ]不匹配[\w]
字符串:[]不匹配[\w]
字符串:[!]不匹配[\w]
字符串:[]不匹配[\w]
/*************\W****************/
字符串:[1]不匹配[\W]
字符串:[2]不匹配[\W]
字符串:[A]不匹配[\W]
字符串:[B]不匹配[\W]
字符串:[a]不匹配[\W]
字符串:[b]不匹配[\W]
字符串:[
]匹配[\W]
字符串:[   ]匹配[\W]
字符串:[
]匹配[\W]
字符串:[ ]匹配[\W]
字符串:[]匹配[\W]
字符串:[!]匹配[\W]
字符串:[]匹配[\W]
/*************\s****************/
字符串:[1]不匹配[\s]
字符串:[2]不匹配[\s]
字符串:[A]不匹配[\s]
字符串:[B]不匹配[\s]
字符串:[a]不匹配[\s]
字符串:[b]不匹配[\s]
字符串:[
]匹配[\s]
字符串:[   ]匹配[\s]
字符串:[
]匹配[\s]
字符串:[ ]匹配[\s]
字符串:[]不匹配[\s]
字符串:[!]不匹配[\s]
字符串:[]不匹配[\s]
/*************\S****************/
字符串:[1]匹配[\S]
字符串:[2]匹配[\S]
字符串:[A]匹配[\S]
字符串:[B]匹配[\S]
字符串:[a]匹配[\S]
字符串:[b]匹配[\S]
字符串:[
]不匹配[\S]
字符串:[   ]不匹配[\S]
字符串:[
]不匹配[\S]
字符串:[ ]不匹配[\S]
字符串:[]匹配[\S]
字符串:[!]匹配[\S]
字符串:[]匹配[\S]

特殊的简记法:点号

  • 点号 “.”是一个特殊的字符组简记法,它可以匹配几乎所有的字符。
  • “\.”匹配点号本身。
  • 在字符组内部,[.]也只能够匹配点号本身。
  • 注意:点号不能够匹配换行符及回车符但能匹配制表符。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li4 {
         public static void main(String[] args) {
            String rex1=".";
            String rex2="\\.";
            String rex3="[.]";

            String [] string=new String[]{"1","!",".","。"};

            System.out.println("/*************.****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex1);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex1+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex1+"\"");
                }
            }

            System.out.println("/*************\\.****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex2);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex2+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex2+"\"");
                }
            }

            System.out.println("/*************[.]****************/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex3);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex3+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex3+"\"");
                }
            }
        }
}

运行结果:

/*************.****************/
字符串:"1"匹配"."
字符串:"2"匹配"."
字符串:"A"匹配"."
字符串:"B"匹配"."
字符串:"a"匹配"."
字符串:"b"匹配"."
字符串:"
"不匹配"."
字符串:" "匹配"."
字符串:"
"不匹配"."
字符串:" "匹配"."
字符串:"呵"匹配"."
字符串:"!"匹配"."
字符串:"!"匹配"."
字符串:"."匹配"."
字符串:"。"匹配"."
/*************\.****************/
字符串:"1"不匹配"\."
字符串:"2"不匹配"\."
字符串:"A"不匹配"\."
字符串:"B"不匹配"\."
字符串:"a"不匹配"\."
字符串:"b"不匹配"\."
字符串:"
"不匹配"\."
字符串:" "不匹配"\."
字符串:"
"不匹配"\."
字符串:" "不匹配"\."
字符串:"呵"不匹配"\."
字符串:"!"不匹配"\."
字符串:"!"不匹配"\."
字符串:"."匹配"\."
字符串:"。"不匹配"\."
/*************[.]****************/
字符串:"1"不匹配"[.]"
字符串:"2"不匹配"[.]"
字符串:"A"不匹配"[.]"
字符串:"B"不匹配"[.]"
字符串:"a"不匹配"[.]"
字符串:"b"不匹配"[.]"
字符串:"
"不匹配"[.]"
字符串:" "不匹配"[.]"
字符串:"
"不匹配"[.]"
字符串:" "不匹配"[.]"
字符串:"呵"不匹配"[.]"
字符串:"!"不匹配"[.]"
字符串:"!"不匹配"[.]"
字符串:"."匹配"[.]"
字符串:"。"不匹配"[.]"

量词

  • 作用:限定之前的字符出现的次数
  • 形式:
  • “*” :之前的字符可以出现0次到无穷多次
  • “+” :之前的字符可以出现1次到无穷多次
  • “?” :之前的字符至多只能出现1次,即0次或者1次
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li5 {
         public static void main(String[] args) {
            String rex1="a*";
            String rex2="a?";
            String rex3="a+";

            String [] string=new String[]{"","aa","aaa"};

            System.out.println("/--------------a*-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex1);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex1+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex1+"\"");
                }
            }

             System.out.println("/--------------a?-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex2);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex2+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex2+"\"");
                }
            }

                System.out.println("/--------------a+-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex3);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex3+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex3+"\"");
                }
            }
        }
}

运行结果:

/--------------a*-----------------/
字符串:""匹配"a*"
字符串:"a"匹配"a*"
字符串:"aa"匹配"a*"
字符串:"aaa"匹配"a*"
/--------------a?-----------------/
字符串:""匹配"a?"
字符串:"a"匹配"a?"
字符串:"aa"不匹配"a?"
字符串:"aaa"不匹配"a?"
/--------------a+-----------------/
字符串:""不匹配"a+"
字符串:"a"匹配"a+"
字符串:"aa"匹配"a+"
字符串:"aaa"匹配"a+"

区间量词

  • 作用:具体规定字符的出现次数
  • 形式:
  • {min,max}即最小出现min次最多出现max次
  • {min,}即最小出现min次最多为无穷次
  • {number}即规定必须出现number次
  • “*” 相当于 {0,}
  • “+” 相当于{1,}
  • “?” 相当于 {0,1}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li6 {
           public static void main(String[] args) {
               String rex1="a{2,4}";
            String rex2="a{2,}";
            String rex3="a{3}";

            String [] string=new String[]{"","aaa","aaaa","aaaaa","aaaaaa"};

               System.out.println("/--------------a{2,4}-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex1);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex1+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex1+"\"");
                }
            }

           System.out.println("/--------------a{2,}-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex2);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex2+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex2+"\"");
                }
            }

             System.out.println("/--------------a{3}-----------------/");

            for(String str:string){
                Pattern pattern=Pattern.compile(rex3);
                Matcher matcher=pattern.matcher(str);
                if(matcher.matches()){
                    System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex3+"\"");
                }else{
                    System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex3+"\"");
                }
            }
        }
}

运行结果:

/--------------a{2,4}-----------------/
字符串:""不匹配"a{2,4}"
字符串:"a"不匹配"a{2,4}"
字符串:"aa"匹配"a{2,4}"
字符串:"aaa"匹配"a{2,4}"
字符串:"aaaa"匹配"a{2,4}"
字符串:"aaaaa"不匹配"a{2,4}"
字符串:"aaaaaa"不匹配"a{2,4}"
/--------------a{2,}-----------------/
字符串:""不匹配"a{2,}"
字符串:"a"不匹配"a{2,}"
字符串:"aa"匹配"a{2,}"
字符串:"aaa"匹配"a{2,}"
字符串:"aaaa"匹配"a{2,}"
字符串:"aaaaa"匹配"a{2,}"
字符串:"aaaaaa"匹配"a{2,}"
/--------------a{3}-----------------/
字符串:""不匹配"a{3}"
字符串:"a"不匹配"a{3}"
字符串:"aa"不匹配"a{3}"
字符串:"aaa"匹配"a{3}"
字符串:"aaaa"不匹配"a{3}"
字符串:"aaaaa"不匹配"a{3}"
字符串:"aaaaaa"不匹配"a{3}"

量词的局限,括号的使用

  • 量词只能规定之前字符或字符组的出现次数(只能匹配单个字符)
  • 如果要规定一个字符串的出现次数,必须使用括号”()”,在括号内填写字符串,在闭括号之后添加量词。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li7 {
           public static void main(String[] args) {
               String rex1="ac+";
               String rex2="(ac)+";
               String rex3="ac*";
               String rex4="(ac)*";
               String rex5="ac?";
               String rex6="(ac)?";

               String [] string=new String[]{"","ac","acc","accc","acacac","acac","c"};

                 System.out.println("/--------------ac+-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex1);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex1+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex1+"\"");
                        }
                    }

                   System.out.println("/--------------(ac)+-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex2);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex2+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex2+"\"");
                        }
                    }

                     System.out.println("/--------------ac*-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex3);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex3+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex3+"\"");
                        }
                    }


                     System.out.println("/--------------(ac)*-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex4);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex4+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex4+"\"");
                        }
                    }

                     System.out.println("/--------------ac?-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex5);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex5+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex5+"\"");
                        }
                    }

                 System.out.println("/--------------(ac)?-----------------/");

                    for(String str:string){
                        Pattern pattern=Pattern.compile(rex6);
                        Matcher matcher=pattern.matcher(str);
                        if(matcher.matches()){
                            System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex6+"\"");
                        }else{
                            System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex6+"\"");
                        }
                    }
        }
}

运行结果:

/--------------ac+-----------------/
字符串:""不匹配"ac+"
字符串:"ac"匹配"ac+"
字符串:"acc"匹配"ac+"
字符串:"accc"匹配"ac+"
字符串:"acacac"不匹配"ac+"
字符串:"acac"不匹配"ac+"
字符串:"a"不匹配"ac+"
字符串:"c"不匹配"ac+"
/--------------(ac)+-----------------/
字符串:""不匹配"(ac)+"
字符串:"ac"匹配"(ac)+"
字符串:"acc"不匹配"(ac)+"
字符串:"accc"不匹配"(ac)+"
字符串:"acacac"匹配"(ac)+"
字符串:"acac"匹配"(ac)+"
字符串:"a"不匹配"(ac)+"
字符串:"c"不匹配"(ac)+"
/--------------ac*-----------------/
字符串:""不匹配"ac*"
字符串:"ac"匹配"ac*"
字符串:"acc"匹配"ac*"
字符串:"accc"匹配"ac*"
字符串:"acacac"不匹配"ac*"
字符串:"acac"不匹配"ac*"
字符串:"a"匹配"ac*"
字符串:"c"不匹配"ac*"
/--------------(ac)*-----------------/
字符串:""匹配"(ac)*"
字符串:"ac"匹配"(ac)*"
字符串:"acc"不匹配"(ac)*"
字符串:"accc"不匹配"(ac)*"
字符串:"acacac"匹配"(ac)*"
字符串:"acac"匹配"(ac)*"
字符串:"a"不匹配"(ac)*"
字符串:"c"不匹配"(ac)*"
/--------------ac?-----------------/
字符串:""不匹配"ac?"
字符串:"ac"匹配"ac?"
字符串:"acc"不匹配"ac?"
字符串:"accc"不匹配"ac?"
字符串:"acacac"不匹配"ac?"
字符串:"acac"不匹配"ac?"
字符串:"a"匹配"ac?"
字符串:"c"不匹配"ac?"
/--------------(ac)?-----------------/
字符串:""匹配"(ac)?"
字符串:"ac"匹配"(ac)?"
字符串:"acc"不匹配"(ac)?"
字符串:"accc"不匹配"(ac)?"
字符串:"acacac"不匹配"(ac)?"
字符串:"acac"不匹配"(ac)?"
字符串:"a"不匹配"(ac)?"
字符串:"c"不匹配"(ac)?"

括号的用途:多选结构

  • 字符组只能表示某个位置可能出现的单个字符,而不能表示某个位置可能出现的字符串。
  • 作用:表示某个位置可能出现的字符串,或者可能出现的多个字符串。
  • 形式:
  • “(…|…)”,在竖线两端添加各个字符串
  • “(…|…|…|…)”
public class li8 {
      public static void main(String[] args) {
          String [] string=new String[]{"this","that","thit"};

          String rex="th[ia][st]";

          Pattern pattern=Pattern.compile(rex);

          for(String str:string){
              Matcher matcher=pattern.matcher(str);
              if(matcher.matches()){
                  System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex+"\"");
              }else{
                  System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex+"\"");
              }
          }
    }
}

运行结果:

字符串:"this"匹配"th[ia][st]"
字符串:"that"匹配"th[ia][st]"
字符串:"thit"匹配"th[ia][st]"

而我们不想匹配错误的单词thit怎么办呢??
看例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li8 {
      public static void main(String[] args) {
          String [] string=new String[]{"this","thit"};

          String rex="this|that";//也可以写成th(is|at) 这样把公共提出来后可以提高正则匹配效率。

          Pattern pattern=Pattern.compile(rex);

          for(String str:string){
              Matcher matcher=pattern.matcher(str);
              if(matcher.matches()){
                  System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex+"\"");
              }else{
                  System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex+"\"");
              }
          }
    }
}

运行结果:

字符串:"this"匹配"this|that"
字符串:"that"匹配"this|that"
字符串:"thit"不匹配"this|that"

括号的用途:捕获分组

  • 作用:将括号内的子表达式捕获的字符串存放到匹配的结果中,供匹配完成后访问。
  • 形式: 使用普通的括号”(…)”。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li9 {
      public static void main(String[] args) {
         String email="webmaster@itcast.net";

         String rex="(\\w+)@([\\w.]+)";

         Pattern pattern=Pattern.compile(rex);

         Matcher matcher=pattern.matcher(email);

         if(matcher.find()){
             System.out.println("email add is:\t"+matcher.group(0));//默认打印的是整个匹配的结果
             System.out.println("username is:\t"+matcher.group(1));//打印从左到右第一个括号内的结果
             System.out.println("hostname is:\t"+matcher.group(2));//打印从左到右第二个括号内的结果
         }
    }
}

运行结果:

email add is:   webmaster@itcast.net
username is:    webmaster
hostname is:    itcast.net

需要强调的是:括号的先后顺序按照左括号的出现顺序编号,编号从1开始。编号0为整个正则表达式的匹配结果!!

捕获分组的注意事项

  • 只要使用了括号,就存在捕获分组。
  • 捕获分组按照开括号出现的从左到右的顺序编号,编号从1开始,遇到括号嵌套的情况也是如此。
  • 如果捕获分组之后存在量词,则匹配结果中,捕获分组保存的是子表达式最后一次的匹配字符串。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li10 {

    public static void main(String[] args) {

        explainGroupNo();

        System.out.println("");

        explainGroupQuantifier();       

    }

    public static void explainGroupNo() {

        String email = "webmaster@itcast.net";

        String regex = "((\\w+)@([\\w.]+))";

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(email);

        if (m.find()) {
            System.out.println("match result:\t" + m.group(0));
            System.out.println("group No.1 is:\t" + m.group(1));
            System.out.println("group No.2 is:\t" + m.group(2));
            System.out.println("group No.3 is:\t" + m.group(3));
        }
    }

    public static void explainGroupQuantifier() {
        String email = "webmaster@itcast.net";

        String regex = "(\\w)+@([\\w.])+";

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(email);

        if (m.find()) {
            System.out.println("match result:\t" + m.group(0));
            System.out.println("group No.1 is:\t" + m.group(1));
            System.out.println("group No.2 is:\t" + m.group(2));
        }
    }

}

运行结果:

match result:   webmaster@itcast.net
group No.1 is:  webmaster@itcast.net
group No.2 is:  webmaster
group No.3 is:  itcast.net

match result:   webmaster@itcast.net
group No.1 is:  r
group No.2 is:  t

不捕获文本的括号

  • 如果正则表达式很复杂,或者需要处理的文本很长,捕获分组会降低效率。
  • 作用:仅仅用来对表达式分组,而不把分组捕获的文本存入结果。
  • 形式:”(?:…)”。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li11 {
           public static void main(String[] args) {
                String email = "webmaster@itcast.net";

                String rex="(?:webmaster|admin)@([\\w.]+)";

                Pattern pattern=Pattern.compile(rex);

                Matcher matcher=pattern.matcher(email);

                if(matcher.find()){
                    System.out.println("match result:\t"+matcher.group(0));
                    System.out.println("group No.1 is:\t" + matcher.group(1));
                    //System.out.println(matcher.group(2));
                }
        }
    }

运行结果:

match result:   webmaster@itcast.net
group No.1 is:  itcast.net

因为只要出现了括号就会存在捕获分组,并且会保存捕获结果,但是使用(?:…)就不会保存捕获结果了。所以当要输出编号为1的时候就输出了第二个括号的内容。而不会输出第一个括号的捕获内容,因为第一个括号的捕获内容不会保存!!编号0依然是整个正则表达式匹配的内容。

括号的用途:反向引用

  • 作用:在表达式的某一部分,动态重复之前的子表达式所匹配的文本。
  • 形式:”\1” 其中的1为捕获分组的编号。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li12 {
        public static void main(String[] args) {
              String [] string=new String[]{"<h1>呵呵</h1>","<h1>123</h2>"};

              String rex="<(\\w+)>[^<]+<(/\\1)>";

              Pattern pattern=Pattern.compile(rex);

              for(String str:string){
                  Matcher matcher=pattern.matcher(str);
                  if(matcher.matches()){
                      System.out.println("字符串:"+"\""+str+"\""+"匹配\""+rex+"\"");
                  }else{
                      System.out.println("字符串:"+"\""+str+"\""+"不匹配\""+rex+"\"");
                  }
              }
        }
}

运行结果:

字符串:"<h1>呵呵</h1>"匹配"<(\w+)>[^<]+<(/\1)>" 字符串:"<h1>123</h2>"不匹配"<(\w+)>[^<]+<(/\1)>"

例子:去掉重复单词

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li13 {
            public static void main(String[] args) {        

                One();
                System.out.println();
                Two();          

            }

            private static void Two() {
                String string="hello hello";

                String rex="((\\w+)\\s+\\2)";

                Pattern pattern=Pattern.compile(rex);
                Matcher matcher=pattern.matcher(string);

                System.out.println("字符串:"+string);

                System.out.println("去掉后:"+matcher.replaceAll("$2"));//美元符号可视为到如上所述已捕获子序列的引用

            }

            private static void One() {
                String string="java java";
                String rex="(\\w+)\\s+\\1";
                System.out.println("字符串:"+string);
                System.out.println("去掉后:"+string.replaceAll(rex,"$1"));//美元符号可视为到如上所述已捕获子序列的引用 
            }
}

运行结果:

字符串:java java
去掉后:java

字符串:hello hello
去掉后:hello

到了这个阶段,我想读者也有一定能力了。来看看这个文章:

强调在正则中只要单纯用了括号就会有捕获分组保存

锚点

  • 作用:规定匹配的位置。
  • 形式: “\b”单词分界符锚点。
  • 单词分界符的意思是划分单词和符号之间的界限。注意:这些符号还包括换行、空格、制表符、回车符。当然读者不要想多了,符号就只有空格那几个,符号还包括中文的符号和英文的符号如”!”,”!”,英文感叹号,中文感叹号!!!等,你所
    知道的一切符号!!!

例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li15 {
       public static void main(String[] args) {
           String [] strings=new String[] 
                   {"我爱 java ","我爱 html5 ","我爱 \"java\"","我爱 javase"
                   };

           String rex="\\bjava\\b";

           Pattern pattern=Pattern.compile(rex);

           for(String str:strings){
               Matcher matcher=pattern.matcher(str);
               if(matcher.find()){
                   System.out.println("字符串:["+str+"]有");
               }else{
                   System.out.println("字符串:["+str+"]没有");
               }
               System.out.println();
           }
    }
}

运行结果:

字符串:[我爱 java ]有

字符串:[我爱 html5 ]没有

字符串:[我爱 "java"]有

字符串:[我爱 javase]没有

注意事项:

  • \b表示单词分界符,要求一侧是单词字符,另一侧是非单词字符。
  • 单词字符通常是指英文字符、数字字符和中文。
  • 非单词字符通常指的是各种标点符号和空白字符。
  • 如果上面这三条注意事项不是很明白,可以看这篇博客详解正则表达式中的\B和\b

锚点二

  • “^” 匹配一行的开头(有可能变化),但是在没有设置匹配模式的情况下(即默认情况下),它是匹配整个字符串的开头和\A一样。
  • “$” 匹配一行的末尾(有可能变化)但是在没有设置匹配模式的情况下(即默认情况下),它是匹配整个字符串的结尾和\Z一样。
  • “\A” 匹配整个字符串的开头
  • “\Z” 匹配整个字符串的末尾
  • 如果想要 “^”和 “$”在一整段字符串中匹配逻辑行的开头或者结尾的话,需要修改匹配模式。
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li16 {
          public static void main(String[] args) {
              String[] strings = new String[] { "start "," start "," end "," end" };

              String[] regexes = new String[] { "^start","\\Astart","end$","end\\Z"};

              for (String str : strings) {
                  for (String regex : regexes) {
                      Pattern p = Pattern.compile(regex);
                      Matcher m = p.matcher(str);
                      if(m.find()) {
                          System.out.println("\"" + str
                                  + "\" can be matched with regex \"" + regex
                                  + "\"");
                      }
                      else {
                          System.out.println("\"" + str
                                  + "\" can not be matched with regex \"" + regex
                                  + "\"");
                      }
                  }
                  System.out.println("");
              }
        }
}

运行结果:

"start " can be matched with regex "^start"
"start " can be matched with regex "\Astart"
"start " can not be matched with regex "end$"
"start " can not be matched with regex "end\Z"

" start " can not be matched with regex "^start"
" start " can not be matched with regex "\Astart"
" start " can not be matched with regex "end$"
" start " can not be matched with regex "end\Z"

" end " can not be matched with regex "^start"
" end " can not be matched with regex "\Astart"
" end " can not be matched with regex "end$"
" end " can not be matched with regex "end\Z"

" end" can not be matched with regex "^start"
" end" can not be matched with regex "\Astart"
" end" can be matched with regex "end$"
" end" can be matched with regex "end\Z"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jquery.validate使用攻略(表单校验) 目录 jquery.validate使用攻略1 第一章&#160;jquery.validate使用攻略1 第二章&#160;jQuery.validate.js API7 Custom selectors7 Utilities8 Validato
/\s+/g和/\s/g的区别 正则表达式/\s+/g和/\s/g,目的均是找出目标字符串中的所有空白字符,但两者到底有什么区别呢? 我们先来看下面一个例子: let name = &#39;ye wen jun&#39;;let ans = name.replace(/\s/g, &#39;&#3
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母 /^[0-9a-zA-Z]*$/g jQuery.validator.addMethod(&quot;letters&quot;, function (value, element) { return this.optio
this.optional(element)的用法 this.optional(element)是jquery.validator.js表单验证框架中的一个函数,用于表单控件的值不为空时才触发验证。 简单来说,就是当表单控件值为空的时候不会进行表单校验,此函数会返回true,表示校验通过,当表单控件
jQuery.validate 表单动态验证 实际上jQuery.validate提供了动态校验的方法。而动态拼JSON串的方式是不支持动态校验的。牺牲jQuery.validate的性能优化可以实现(jQuery.validate的性能优化见图1.2 jQuery.validate源码 )。 也可
自定义验证之这能输入数字(包括小数 负数 ) &lt;script type=&quot;text/javascript&quot;&gt; function onlyNumber(obj){ //得到第一个字符是否为负号 var t = obj.value.charAt(0); //先把非数字的都
// 引入了外部的验证规则 import { validateAccountNumber } from &quot;@/utils/validate&quot;; validator.js /*是否合法IP地址*/ export function validateIP(rule, value,cal
VUE开发--表单验证(六十三) 一、常用验证方式 vue 中表单字段验证的写法和方式有多种,常用的验证方式有3种: data 中验证 表单内容: &lt;!-- 表单 --&gt; &lt;el-form ref=&quot;rulesForm&quot; :rules=&quot;formRul
正则表达式 座机的: 例子: 座机有效写法: 0316-8418331 (010)-67433539 (010)67433539 010-67433539 (0316)-8418331 (0316)8418331 正则表达式写法 0\d{2,3}-\d{7,8}|\(?0\d{2,3}[)-]?\d
var reg = /^0\.[1-9]{0,2}$/;var linka = 0.1;console.log (reg.test (linka)); 0到1两位小数正则 ^(0\.(0[1-9]|[1-9]{1,2}|[1-9]0)$)|^1$ 不含0、0.0、0.00 // 验证是否是[1-10
input最大长度限制问题 &lt;input type=&quot;text&quot; maxlength=&quot;5&quot; /&gt; //可以 &lt;input type=&quot;number&quot; maxlength=&quot;5&quot; /&gt; //没有效
js输入验证是否为空、是否为null、是否都是空格 目录 1.截头去尾 trim 2.截头去尾 会去掉开始和结束的空格,类似于trim 3.会去掉所有的空格,包括开始,结束,中间 1.截头去尾 trim str=str.trim(); // 强烈推荐 最常用、最实用 or $.trim(str);
正则表达式语法大全 字符串.match(正则):返回符合的字符串,若不满足返回null 字符串.search(正则):返回搜索到的位置,若非一个字符,则返回第一个字母的下标,若不匹配则返回-1 字符串.replace(正则,新的字符串):找到符合正则的内容并替换 正则.test(字符串):在字符串中
正整数正则表达式正数的正则表达式(包括0,小数保留两位): ^((0{1}.\d{1,2})|([1-9]\d.{1}\d{1,2})|([1-9]+\d)|0)$正数的正则表达式(不包括0,小数保留两位): ^((0{1}.\d{1,2})|([1-9]\d.{1}\d{1,2})|([1-9]+
JS 正则验证 test() /*用途:检查输入手机号码是否正确输入:s:字符串返回:如果通过验证返回true,否则返回false /function checkMobile(s){var regu =/[1][3][0-9]{9}$/;var re = new RegExp(regu);if (r
请输入保留两位小数的销售价的正则: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/ 1.只能输入英文 &lt;input type=&quot;text&quot; onkeyup=&quot;value
判断价格的正则表达式 价格的正则表达式 /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/; 1 解析:价格符合两种格式 ^ [1-9]\d*(.\d{1,2})?$ : 1-9 开头,后跟是 0-9,可以跟小数点,但小数点后要带上 1-2 位小数,类似 2,2
文章浏览阅读106次。这篇文章主要介绍了最实用的正则表达式整理,比如校验邮箱的正则,号码相关,数字相关等等,本文给大家列举的比较多,需要的朋友可以参考下。_/^(?:[1-9]d*)$/ 手机号
文章浏览阅读1.2k次。4、匹配中的==、an==、== an9、i9 == "9i"和99p==请注意下面这部分的作用,它在匹配中间内容的时候排除了说明:当html字符串如下时,可以匹配到两处,表示匹配的字符串不包含and且不包含空白字符。说明:在上面的正则表达式中,_gvim正则表达式匹配不包含某个字符串
文章浏览阅读897次。【代码】正则表达式匹配a标签的href。_auto.js 正则匹配herf