java 正则

检测qq是否合法

package com.tz.regexp;

public class QQTest {
    public static void main(String[] args) {
//      testQQ();
        testQQ_1();
    }
    public static void testQQ_1(){
        //验证QQ 不能已0开头  长度5-15位   只能是数字
        String QQ = "12345";
        String reg = "[1-9][0-9]{4,14}";
        boolean flag = QQ.matches(reg);
        if(flag){
            System.out.println("QQ号码正确哦,为:"+QQ);
        }else{
            System.out.println("QQ不合法哦!!!");
        }
    }

    public static void testQQ(){
        String QQ = "12阿334534547";
        int len = QQ.length();
        if(len>=5 && len<=15){//判断字符串的长度为5-15
            if(!QQ.startsWith("0")){//判断首位如果不是0
//              boolean flag = true;
//              char[] array = QQ.toCharArray();
//              for(int i=0;i<array.length;i++){
//                  if(!(array[i]>='0' && array[i]<='9')){//如果QQ号码不是数字,将标示变成false,直接跳出for循环
//                      flag = false;
//                      break;
//                  }
//              }
//              if(flag){//如果标示一直为true,说明QQ正确,否则QQ非法
//                  System.out.println("QQ号码正确"+QQ);
//              }else{
//                  System.out.println("QQ号不规范,即不合法的!!!!!!!!!!!");
//              }

                //采用Long的parseLong(str);
                try {
                    long num = Long.parseLong(QQ);
                    System.out.println("QQ合法"+QQ);
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("QQ 不合法。。。。。。。。");
                }
            }else{
                System.out.println("首字母不能为0!");
            }
        }else{
            System.out.println("长度不对!!!");
        }
    }   
}

Matches的使用

package com.tz.regexp;

public class MatchesDemo {
    public static void main(String[] args) {
        matchesTest();
    }   
    public static void matchesTest(){
        //用String串对象提供的matches(String reg)匹配整个字符串,只有一处不符合就匹配结束,匹配结果返回boolean类型的false,匹配成功返回true
        String str = "a123456";
        String reg = "[a-z]\\d{1,5}";  // \d  ?---->[0,1]   *--->0或者多次[0,)     +---->1次或者多次[1,+)
        System.out.println(str.matches(reg));
    }
}

这里写图片描述

这里写图片描述

Split的使用

package com.tz.regexp;

public class SplitDemo {
    public static void main(String[] args) {
        splitMethod();
    }

    public static void splitMethod(){
//      String string = "张三            李四                           王五";
//      String string = "张三.李四.王五";

        String string = "sdasdqqqqqqqqqqqancaabcgghc";//按照叠词完成切割
        /*
         * (.)\1 将规则封装成为一个组,用()完成
         *  组的出现都有编号,从1开始
         *  要想使用已有的组,可以通过\n(n为组的编号)的形式来获取
         * 
         *  ((()()))---->从左边括号开始是第一组。。4组
         * 
         * */
        String reg = "(.)\\1+"; //分组
        String[] arr = string.split(reg);
        System.out.println(arr.length);
        for(String s:arr){
            System.out.println(s);
        }
    }
}

ReplaceAll的用法

package com.tz.regexp;

public class ReplaceAllDemo {
    public static void main(String[] args) {
//      String str = "abcd023434534adas344asda3454353";
//      replaceAllTest(str,"\\d{3}", "#");

        String str1 = "asdasnnadbbasdsaaa";//将叠词替换成&
//      replaceAllTest(str1,"(.)\\1+","&");
        replaceAllTest(str1,"(.)\\1+","$1");//将叠词只替换成一个表示  aaa--->a

    }
    public static void replaceAllTest(String str,String reg,String newStr){
        str = str.replaceAll(reg, newStr);
        System.out.println(str);
    }
}

Pattren的使用


package com.tz.regexp;

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

public class PatternMatcherDemo {
    public static void main(String[] args) {
//      getDemo();
        getDemo02();
    }

    //获取 :将字符串的符合规则的字串去出
    /*操作步骤:
     *  1.将正则表达式封装成对象   ---》Pattern
     *  2.让正则对象和要操作 的字符串关联
     *  3.关联之后,获取正则匹配器对象(匹配引擎) ---》Matcher
     *  4.通过引擎对符合规则的字串进行操作。。例如可以获取字串
     * */

    public static void getDemo(){
        String string = "12345";
        String reg = "[1-9]\\d{4,14}";
        //将正则表达式封装成pattern对象
        Pattern pattern =Pattern.compile(reg);
        //将正则对象和要操作的字符串关联
        Matcher matcher = pattern.matcher(string);
        //string.matches(reg);
        //String类的对象的matches(reg)方法,底层就是调用的Pattern与Matcher对象完成的,显得比较方便而已。
        System.out.println(matcher.matches());
    }



    public static void getDemo02(){
        String string = "hah jin tian tian qi hao liang shuang,wo hen kai xin ya";
        String reg = "\\b[a-z]{3}\\b";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(string);
//      boolean flag = matcher.find();
//      System.out.println(flag);
//      System.out.println(matcher.group());

        //
        //
        // System.out.println(matcher.matches());//false
        while(matcher.find()){
            System.out.println(matcher.group());
//          System.out.println(matcher.start()+"===="+matcher.end());
        }

    }   
}

邮箱验证

package com.tz.regtest;

public class ExpDemo01 {
    public static void main(String[] args) {
//      getDemo();
        getEmail();
    }
    public static void getDemo(){
        /*
         * 判断字符串是否与正则相匹配   matches(String reg)
         * 把已有的字符串变成另外一个字符串使用替换  str.replaceAll(String reg,String newStr);
         * 把已有的字符串变成多个字符串使用split--->String[]
         * 想要获取符合要求的字符串  ---》reg--->Pattern 与  Matcher
         * */
        //将下面变成今天很舒服 
        String str = "今今..今今天天...很很..........舒舒.....服";
        str = str.replaceAll("\\.+","");
        System.out.println(str);
        str = str.replaceAll("(.)\\1+","$1");
        System.out.println(str);
    }
    public static void getEmail(){
        //[email protected]
        String mail = "[email protected]";
        String reg = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
        System.out.println(mail.matches(reg));
    }
}

爬虫

package com.tz.regtest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExpDemo02 {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:8080/Jery_01/yx.html");
        URLConnection connection = url.openConnection();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = null;
        String mailReg = "\\w+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";
        Pattern pattern = Pattern.compile(mailReg);
        while((line=bReader.readLine())!=null){
            Matcher matcher = pattern.matcher(line);
            while(matcher.find()){
                System.out.println(matcher.group());
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_37243717/article/details/79887746