java正则表达式

正则表达式

 

3.1 正则表达式的作用

用于匹配字符串,比如匹配手机号码,邮箱的格式

3.2 校验QQ

方式一:未使用正则

/*        校验qq号码. 
        1:要求必须是5-15位数字 
        2:0不能开头 
        3:必须都是数字*/
        
        String qq = "10a101";
        
        //1:要求必须是5-15位数字 
        if(qq.length() >=5 && qq.length() <= 15){
            System.out.println("qq号的长度正确 ");
            
            //2: 0不能开头 
            if(!qq.startsWith("0")){
                
                //3.字符中是否有非数字字符
                for(int i=0;i<qq.length();i++){
                    char ch = qq.charAt(i);
                    if( !(ch >= '0' && ch <= '9')){
                        System.out.println("不合法字符:" + ch);
                    }
                }
                
            }else{
                System.out.println("qq号不能以0开头");
            }
        }else{
            System.out.println("qq号码的长度不正确");
        }
    }

方式二:使用正则

//1.qq的正则
        String regex = "[1-9]\\d{4,14}";
        
        //2.打印匹配结果
        System.out.println("1030103135".matches(regex));
        System.out.println("01030103135".matches(regex));
        System.out.println("1030".matches(regex));
        System.out.println("1030103135111112".matches(regex));
        System.out.println("10a30".matches(regex));

3.3正则表达式的构造摘要

字符类

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

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

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

[a-zA-Z_0-9] a 到 z 或 A 到 Z,_,0到9(范围)

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

[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](减去)

 

 

预定义字符类

. 任何字符

\d 数字:[0-9]

\D 非数字: [^0-9]

\s 空白字符:[ \t\n\x0B\f\r]

\S 非空白字符:[^\s]

\w 单词字符:[a-zA-Z_0-9]

\W 非单词字符:[^\w]

 

 

数量词 

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

X* X,零次或多次

X+ X,一次或多次

X{n} X,恰好 n 次

X{n,} X,至少 n 次

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

 

 

 3.4 正则表达式的分割功能

  • public String[] split(String regex)
  • 根据给定正则表达式的匹配拆分此字符串

 

/*String s = "11-23-21-20-28";
        String[] arr = s.split("-");
        for(String str : arr){
            System.out.println(str);
        }*/
        
        String s = "11.23.21.20.28";
        /**
         * 如果是按.拆分,注意要添加转义字符
         */
        String[] arr = s.split("\\.");
        for(String str : arr){
            System.out.println(str);
        }

3.5 正则表达式的替换功能

  • public String replaceAll(String regex,String replacement)
 String s = "520 java 520 h5 520 c";
        
        //把520都改成"我爱你"
        //s = s.replaceAll("\\d{3}", "我爱你");
        s = s.replaceAll("520", "我爱你");
        System.out.println(s);

3.6 Pattern和Matcher

正则的另一种写法

//        检验QQ
//        boolean b1 = "101030".matches("[1-9]\\d{4,14}");
//        System.out.println(b1);
        //等效于上面一行代码
        Pattern p = Pattern.compile("[1-9]\\d{4,14}");//正则
        Matcher m = p.matcher("101030");
        boolean b = m.matches();
        System.out.println(b);

扣出手机字符串

String s = "我的手机是18988888888,我曾用过18987654321,还用过18812345678";
        
        //Matcher 的find和group方法
        
        //1.匹配手机正则表达(11位)
        String regex = "1[3789]\\d{9}";
        
        //2.创建Pattern
        Pattern p = Pattern.compile(regex);
        
        //3.创建Matcher
        Matcher m = p.matcher(s);
        
        //4.找到匹配正则的内容
/*        System.out.println(m.find());
        System.out.println(m.group());
        System.out.println(m.find());
        System.out.println(m.group());
        System.out.println(m.find());
        System.out.println(m.group());*/
        while(m.find()){
            System.out.println(m.group());
        }

 

posted @ 2018-12-22 23:21  expworld  阅读(1770)  评论(0编辑  收藏  举报