Day31-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\Regex-正则表达式+爬虫

正则表达式

image-20251104140210317

image-20251104163925972

package Basic.src.com.Regex;

public class RegexDemo1 {
    public static void main(String[] args) {
        /*校验QQ号是否正确
        * 规则:6位及20位之内,0不能在开头,必须全部是数字
        * 先使用目前所学的知识完成校验需求
        * 然后体验一下正则表达式*/
        String qq = "1234567890";
        //System.out.println(checkQQ(qq));
        System.out.println(qq.matches("[1-9]\\d{5,19}"));
    }
    public static boolean checkQQ(String qq) {
        //规则:6位及20位之内,0不能在开头,必须全部是数字
        //核心思想:
        //先把异常数据进行过滤
        //下面的就是满足要求的数据了。
        int len = qq.length();
        if (len < 6||len>20){//先异常判断
            return false;
        }
        //0不能开头
        if(qq.startsWith("0")){
            return false;
        }
        //必须全部是数字
        for (int i = 0; i < qq.length(); i++) {
            char c = qq.charAt(i);//charAt(i) 是 String 类的一个实例方法,作用是获取字符串中指定索引位置的字符。
            if (c < '0' || c > '9') {//字符单引号比较,比较的是字符的ASCII码值
                return false;
            }
        }
        return true;
    }
}
package Basic.src.com.Regex;

public class RegexDemo2 {
    public static void main(String[] args) {
        //只能是abc
        System.out.println("==========1===========");
        System.out.println("a".matches("[abc]"));//true
        System.out.println("z".matches("[abc]"));//false
        System.out.println("ab".matches("[abc]"));//false,意思是[]中的内容只能出现一个
        System.out.println("ab".matches("[abc][abc]"));//true

        //不能出现abc
        System.out.println("==========2===========");
        System.out.println("a".matches("[^abc]"));
        System.out.println("z".matches("[^abc]"));
        System.out.println("zz".matches("[^abc]"));
        System.out.println("zz".matches("[^abc][^abc]"));

        //a到z,A到Z(包括头尾的范围)
        System.out.println("==========3===========");
        System.out.println("a".matches("[a-zA-z]"));//true
        System.out.println("z".matches("[a-zA-z]"));//true
        System.out.println("aa".matches("[a-zA-z]"));//false
        System.out.println("zz".matches("[a-zA-z]"));//false
        System.out.println("zz".matches("[a-zA-z][a-zA-z]"));//true
        System.out.println("0".matches("[a-zA-z]"));//false
        System.out.println("0".matches("[a-zA-z0-9]"));//true

        //[a-d[m-p]]
        System.out.println("==========4===========");
        System.out.println("a".matches("[a-z[m-p]]"));//true
        System.out.println("d".matches("[a-z[m-p]]"));//true
        System.out.println("m".matches("[a-z[m-p]]"));//true
        System.out.println("p".matches("[a-z[m-p]]"));//true
        System.out.println("e".matches("[a-z[m-p]]"));//false
        System.out.println("0".matches("[a-z[m-p]]"));//false

        //[a-z&&[^bc]] a-z和def的交集。为:d,e,f
        //细节:如果要求两个范围的交集需要写两个符号&&
        //如果写成了一个&,那么此时的&就表示的不是交集了,而是简简单单的&符号,没有任何含义
        System.out.println("==========5===========");
        System.out.println("a".matches("[a-z&[def]]"));//true
        System.out.println("&".matches("[a-z&[def]]"));//true
        System.out.println("&".matches("[a-z&&[def]]"));//false

        System.out.println("d".matches("[a-z&&[def]]"));//true
        System.out.println("a".matches("[a-z&&[def]]"));//false

        //[a-z&&[^bc]]   a-z和非bc的交集。(等同于[ad-z])
        System.out.println("==========6===========");
        System.out.println("a".matches("[a-z&&[^bc]]"));//true
        System.out.println("b".matches("[a-z&&[^bc]]"));//false
        System.out.println("0".matches("[a-z&&[^bc]]"));//false

        //[a-z&&[^m-p]]  a到z和除了m到p的交集,等同于[a-lq-z]
        System.out.println("==========6===========");
        System.out.println("a".matches("[a-z&&[^m-p]]"));//true
        System.out.println("m".matches("[a-z&&[^m-p]]"));//false
        System.out.println("0".matches("[a-z&&[^m-p]]"));//false
    }
}
package Basic.src.com.Regex;

public class RegexDemo3 {
    public static void main(String[] args) {
        //   \转义字符  改变后面那个字符原本的含义
        //练习:以字符串的形式打印一个双引号

        //此时这里的\是转义字符,改变了后面那个''双引号原本的含义
        //把它变成了一个普通的双引号而已
        //  \\第一个\是转义字符,改变了后面那个\原本的含义
        System.out.println("\"");



        //.表示任意一个字符
        System.out.println("你".matches("."));//true
        System.out.println("你".matches(".."));//false
        System.out.println("你a".matches(".."));//true

        //  \d表示任意的一个数字,所以\\d在java中要这么表示
        //简单记就是两个\\表示一个\
        System.out.println("a".matches("\\d"));//false
        System.out.println("3".matches("\\d"));//true
        System.out.println("333".matches("\\d"));//false
        System.out.println("333".matches("\\d\\d\\d"));//true

        //  \\w只能是单词字符[a-zA-Z_0-9]
        System.out.println("z".matches("\\w"));//true
        System.out.println("2".matches("\\w"));//true
        System.out.println("21".matches("\\w"));//false
        System.out.println("你".matches("\\w"));//false
        System.out.println("_".matches("\\w"));//true

        //  \\W非单词字符
        System.out.println("你".matches("\\W"));//true
        System.out.println("======================================");

        //必须是数字 字母 下划线 至少六位
        System.out.println("2442fsfsf".matches("\\w{6,}"));//true
        System.out.println("24sf".matches("\\w{6,}"));//false

        //必须是数字和字符 必须是四位
        System.out.println("23dF".matches("a-zA-Z0-9{4}"));//true
        System.out.println("23_F".matches("a-zA-Z0-9{4}"));//false
        System.out.println("23dF".matches("[\\w&&[^_]]{4}"));//true
        System.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false
    }
}
package Basic.src.com.Regex;

public class RegexDemo4 {
    public static void main(String[] args) {
        /*
        * 需求
        * 13112345678   13712345667     13945679027     139456790271
        * 020-2324242     02122442     027-42424    0712-3242434
        * 3232323@qq.com    zhangsan@itcast.cnn     dlei0009@163.com    dlei0009@pci.com.cn
        * */

        //心得:从左到右一个个看
        //第一部分:1表示手机号码只能以1开头
         //第二部分:[3-9],表示手机号码第二位只能是3-9之间
         //第三部分:\\d{9} 表示任意数字可以出现且仅可以出现9次
        System.out.println("================手机号码====================");
        String regex1 = "1[3-9]\\d{9}";
        System.out.println("13112345678".matches(regex1));
        System.out.println("13712345667".matches(regex1));
        System.out.println("13945679027".matches(regex1));
        System.out.println("139456790271".matches(regex1));

        System.out.println("================座机号码====================");
        //一:区号  0[1-9]{2,3}
        //0表示一定是以0开头
        //[1-9]{2,3}:表示区号从第二位开始可以是任意的1-9,且可以出现2-3次
        //二:-?次数0次或1次
         //三:总长度5-10位
        String regex2 = "0[1-9]{2,3}-?[1-9]\\d{4,9}";
        System.out.println("020-2324242".matches(regex2));
        System.out.println("02122442".matches(regex2));
        System.out.println("027-42424".matches(regex2));
        System.out.println("0712-3242434".matches(regex2));

        //3232323@qq.com    zhangsan@itcast.cnn     dlei0009@163.com    dlei0009@pci.com.cn
        //第一部分:@的左边 \\w+:任意的字母或者下划线至少可以出现一次
        //                  \\w:[a-z    A-Z     0-9]
         //第二部分:@只能出现一次
         //第三部分:再分三小段
        //3.1   .的左边[\\w&&[^_]]{2,6}表示任意字符去掉下划线,总共出现2-6次
        //3.2   .需要转义\\.第一个\转义了第二个,使得第二个才是转义.的  \\.
        //3..3  大写字母跟小写字母都可以,只能出现2-3次   [a-zA-Z]{2,3}
        //把3.2和3.3看作一组,可以反复出现1-2次
        System.out.println("================邮箱====================");
        String regex3 = "\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}";
        System.out.println("3232323@qq.com".matches(regex3));
        System.out.println("zhangsan@itcast.cnn".matches(regex3));
        System.out.println("dlei0009@163.com".matches(regex3));
        System.out.println("dlei0009@pci.com.cn".matches(regex3));

        //实际开发中很少会自己写正则表达式
        //百度一个类似的,改成自己想要的
        //String regex4 = "/^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/";^和$表示从开头到末尾,但是java的matchs是默认的 ?:
        String regex4 = "([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d";
        //[01]\d|2[0-3]     |是或者的意思
        System.out.println("23:11:11".matches(regex4));
        System.out.println("00:00:00".matches(regex4));
        String regex5 = "([01]\\d|2[0-3])(:[0-5]\\d){2}";
        System.out.println("23:11:11".matches(regex5));
    }
}

分组

image-20251105155508657

捕获分组

package Basic.src.com.Regex;

import java.util.regex.Pattern;

public class RegexDemo12 {
    public static void main(String[] args) {
        //判断始末是否一致
        //\\:表示组号,表示把第x组的内容拿出来再用一次
        String regex1 = "(.).+\\1";
        System.out.println("a123a".matches(regex1));
        System.out.println("b456b".matches(regex1));
        System.out.println("17891".matches(regex1));
        System.out.println("&789&".matches(regex1));
        System.out.println("a123b".matches(regex1));//false

        System.out.println("================================");
        String regex2 = "(.+).+\\1";
        System.out.println("abc123abc".matches(regex2));
        System.out.println("b456b".matches(regex2));
        System.out.println("123789123".matches(regex2));
        System.out.println("abc123abd".matches(regex2));//false

        System.out.println("================================");
        //一致:捕获分组
        //*表示0次或多次
        //(.)   把首字母看作一组
        //\\2*  首字母看作一组,且可再出现0次或多次
        String regex3 = "((.)\\2*).+\\1";
        System.out.println("aaa123aaa".matches(regex3));
        System.out.println("b456b".matches(regex3));
        System.out.println("111123111".matches(regex3));
        System.out.println("&&abc&&".matches(regex3));
        System.out.println("aaa123aab".matches(regex3));
    }
}

口吃替换

package Basic.src.com.Regex;

public class RegexDemo13 {
    public static void main(String[] args) {
        String str  = "我要学学学编编编编程程程程程";
        //把重复的内容替换为单个的
        //(.)表示把重复内容中的第一个字符看做一组
        String res = str.replaceAll("(.)\\1+", "$1");//replace不识别正则表达式
        System.out.println(res);
//$1指的是正则表达式子外,也就是前面的正则表达式里面的第一组拿出来用
    }
}

image-20251105163306572

package Basic.src.com.Regex;

public class RegexDemo14 {
    public static void main(String[] args) {
        /*
         * 身份证号码:
         * 41080119930228457x
         * 510801197609022309
         * 15040119810705387X
         * 130133197204039024
         * 430102197606046442
         * */
        //非捕获分组:仅仅把数据括起来
        //比如(?:\d|X|x)      (?=\d|X|x)      (?!\d|X|x)
        //(?:\d|X|x)用的多一点
        //特点:不占用组号
        String regex = "[1-9]\\d{16}(?:\\d|X|x)";
    }
}

image-20251105164623780

posted @ 2025-11-05 17:08  David大胃  阅读(3)  评论(0)    收藏  举报