正则表达式:由一些特定的符号组成,代表一个规则

作用一:检验数据格式是否合法,例如邮箱、QQ号、电话等

public class test {

    public static void main(String[] args) {
        System.out.println(checkQQ(null));                // false
        System.out.println(checkQQ("251425876"));         // true
        System.out.println(checkQQ("2514a8d76"));         // false
        System.out.println("---------------------------------------------------");
        System.out.println(checkQQ1(null));               // false
        System.out.println(checkQQ1("251425876"));        // true
        System.out.println(checkQQ1("2514a8d76"));        // false
    }

    // 使用正则表达式验证QQ号码
    public static boolean checkQQ1(String qq) {
        return qq != null && qq.matches("[1-9]\\d{5,19}");
    }

    // 使用字符串操作验证QQ号码
    public static boolean checkQQ(String qq) {
        // 判断qq号码是否为null
        if (qq == null || qq.startsWith("0") || qq.length() < 6 || qq.length() > 20) {
            return false;
        }

        // qq号码不为null,不以0开头,满足6~20之间的长度
        // 判断是否全部为数字
        for (int i = 0; i < qq.length(); i++) {
            char ch = qq.charAt(i);
            if (!Character.isDigit(ch)) {
                return false;
            }
        }
        return true;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

2.一些注意事项,看看就行

public class test {

    public static void main(String[] args) {
        // 尝试匹配字符串 "ab" 是否符合正则表达式 "[a-zA-Z0-9]"
        // 由于正则表达式 "[a-zA-Z0-9]" 只能匹配一个字符,所以会返回 false
        boolean matches = "ab".matches("[a-zA-Z0-9]");
        System.out.println(matches); // 输出: false
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
public class test {

    public static void main(String[] args) {
        //因为在Java中/d有特殊含义,所以要用\\d来匹配单个数字
        // 使用正则表达式 "\\d" 检查单个数字
        System.out.println("1".matches("\\d")); // 输出: true

        // 检查多个数字,\\d 仅能匹配单个数字
        System.out.println("12".matches("\\d")); // 输出: false
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
public class test {

    public static void main(String[] args) {
        // 使用 ? 量词,代表0次或1次
        System.out.println("a".matches("\\w?"));  // 输出: true
        System.out.println("1".matches("\\w?"));  // 输出: true
        System.out.println("abc".matches("\\w?")); // 输出: false

        // 使用 * 量词,代表0次或多次
        System.out.println("abc12".matches("\\w*"));    // 输出: true
        System.out.println("".matches("\\w*"));         // 输出: true
        System.out.println("abc12张".matches("\\w*"));   // 输出: false

        // 使用 + 量词,代表1次或多次
        System.out.println("abc12".matches("\\w+"));    // 输出: true
        System.out.println("".matches("\\w+"));         // 输出: false
        System.out.println("abc12张".matches("\\w+"));   // 输出: false

        // 使用 {3} 量词,表示必须正好匹配 3 次
        System.out.println("a3c".matches("\\w{3}"));    // 输出: true
        System.out.println("abcd".matches("\\w{3}"));   // 输出: false

        // 使用 {3,} 量词,表示至少匹配 3 次
        System.out.println("abcde".matches("\\w{3,}"));  // 输出: true
        System.out.println("ab".matches("\\w{3,}"));    // 输出: false

        // 使用 {3,9} 量词,表示匹配 3 到 9 次
        System.out.println("abc232d".matches("\\w{3,9}")); // 输出: true
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
public class test {

    public static void main(String[] args) {
        // (?i) 忽略大小写
        System.out.println("abc".matches("(?i)abc")); // 输出: true
        System.out.println("ABC".matches("(?i)abc")); // 输出: true
        System.out.println("aBc".matches("a((?i)b)c")); // 输出: true
        System.out.println("ABC".matches("a((?i)b)c")); // 输出: false

        // 要求至少包括三位数字,至少三位字母
        System.out.println("123".matches("\\d{3}[a-z]{3}")); // 输出: false
        System.out.println("123abc".matches("\\d{3}[a-z]{3}")); // 输出: true
        System.out.println("aac".matches("\\d{3}[a-z]{3}")); // 输出: false

        // 必须是“我爱编程”,中间可以任意多个“6”,最后至少是一个“66”
        System.out.println("我爱编程666666".matches("我爱(编程)+(6{2,})+")); // 输出: true
        System.out.println("我爱编程编程6".matches("我爱(编程)+(6{2,})+")); // 输出: false
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

3.案例:检测用户输入的电话、邮箱、时间是否合法。

public class test {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 验证电话号码
        System.out.print("请输入电话号码(格式:123-4567-8901):");
        String phoneNumber = scanner.nextLine();
        System.out.println("电话号码合法性:" + isValidPhoneNumber(phoneNumber));

        // 验证电子邮件
        System.out.print("请输入电子邮件地址:");
        String email = scanner.nextLine();
        System.out.println("电子邮件合法性:" + isValidEmail(email));

        // 验证时间格式
        System.out.print("请输入时间(24小时格式,格式:HH:mm):");
        String time = scanner.nextLine();
        System.out.println("时间合法性:" + isValidTime(time));
        
        scanner.close();
    }

    // 验证电话号码
    public static boolean isValidPhoneNumber(String phone) {
        String regex = "^\\d{3}-\\d{4}-\\d{4}$"; // 格式:123-4567-8901
        return phone.matches(regex);
    }

    // 验证电子邮件
    public static boolean isValidEmail(String email) {
        String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; // 简单的邮箱格式
        return email.matches(regex);
    }

    // 验证时间格式
    public static boolean isValidTime(String time) {
        String regex = "^([01]\\d|2[0-3]):[0-5]\\d$"; // 格式:HH:mm
        return time.matches(regex);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.

这玩意太复杂了,我学不了这东西,对逻辑的的极值判断。

3.用于查找信息,这是固定的内容。

public class test {

    public static void main(String[] args) {
        String data = "来学习Java, \n" +
                      "电话:18666688888, 18699997777\n" +
                      "或者联系邮箱:boniu@itcast.cn, \n" +
                      "座机电话:01036517895, 010-98951256\n" +
                      "邮箱:bozai@itcast.cn, \n" +
                      "邮箱2:dleio009@163.com, \n" +
                      "热线电话:400-618-9090,400-618-4000, 4006184000, 4006189090";

        // 定义正则规则
        String regex = "(\\w{1,}[\\w\\.-]*@[\\w\\.-]+\\.\\w{2,3})|" +  // 邮箱
                       "(1[3-9]\\d{9})|" +  // 手机号
                       "(0\\d{2,3}-?\\d{7,8})|" +  // 座机号
                       "(400-?\\d{3}-?\\d{4})";  // 热线电话

        // 创建Pattern对象
        Pattern pattern = Pattern.compile(regex);

        // 创建Matcher对象
        Matcher matcher = pattern.matcher(data);

        // 匹配并输出结果
        while (matcher.find()) {
            System.out.println(matcher.group().trim());
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

4.对字符串进行替换和分割。

public class test {
    public static void main(String[] args) {
        // 渲染部分 1
        String s1 = "古力娜扎li88888迪丽热巴999aa5566马尔扎哈fbfbfsfs42425卡尔扎巴";
        System.out.println("替换数字: " + s1.replaceAll("\\d+", "-"));

        // 渲染部分 2
        String s2 = "我我我我喜欢编编编编编编编编程编程编程!";
        System.out.println("消除重复: " + s2.replaceAll("(.)\\1+", "$1"));

        // 渲染部分 3
        String s3 = "古力娜扎li88888迪丽热巴999aa5566马尔扎哈fbfbfsfs42425卡尔扎巴";
        String[] names = s3.split("\\d+");
        System.out.println("分割结果:");
        for (String name : names) {
            System.out.println(name);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.