Java SE 学习笔记(七)—— 正则表达式

1 引言


😍 正则表达式可以用一些规定的字符来制定规则,并用来校验数据格式的合法性。

在这里插入图片描述

2 常用匹配规则


2.1 字符类


默认匹配一个字符

[abc] :只能是a 、b、或者 c

[^abc]: 除了a 、b、 c 之外的任意字符

[a-zA-Z]: a 到 z A 到 Z(范围)

[a-d[m-p]] :a 到 d 或者 m 到 p(并集)

[a-z&&[def]]: d、e 或 f (交集)

[a-z&&[^bc]]: a 到 z,除了 b、c

[a-z&&[^m-p]] : a 到 z,除了 m 到 p

2.2 预定义的字符类


默认匹配一个字符

.:任意字符

\d:一个数字:[0-9]

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

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

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

\w:英文、数字、下划线: [a-zA-Z_0-9]

\W:非英文、数字、下划线: [^\w]

2.3 贪婪的量词


配合匹配多个字符

?:前面的字符出现 0 次或 1 次

*:前面的字符出现 0 次或 多 次

+:前面的字符出现 1 次或 多 次

{n}:前面的字符出现 n 次

{n,}:前面的字符至少出现 n 次

{n,m}:前面的字符至少出现 n 次,但不超过 m 次

3 正则表达式匹配的 API


字符串对象提供了匹配正则表达式规则的 API

public boolean matches(String regex)

判断指定字符串是否匹配正则表达式,匹配返回true、不匹配返回false

🙋举个栗子:

public class Demo {
    public static void main(String[] args) {
        // 只能出现a、b、c
        System.out.println("a".matches("[abc]")); // true
        // 默认只匹配一个字符
        System.out.println("ab".matches("[abc]")); // false
        System.out.println("ab".matches("[abc]+")); // true

        // 不能出现a、b、c
        System.out.println("a".matches("[^abc]")); // false
        System.out.println("z".matches("[^abc]")); // true

        // 匹配数字
        System.out.println("3".matches("\\d")); // true
        System.out.println("a".matches("\\d")); // false

        // 匹配数字、字母、_
        System.out.println("_".matches("\\w")); // true
        System.out.println("24".matches("\\w")); // false
        System.out.println("我".matches("\\w")); // false
        System.out.println("我".matches("\\W")); // true

        System.out.println("-------------------------");

        // 校验密码:必须是数字、字母、下划线,至少6位
        System.out.println("2ugyftyf2".matches("\\w{6,}")); // true

        // 验证码,必须是数字、字符,必须是4位
        System.out.println("34jh".matches("[a-zA-Z0-9]{4}")); // true
        System.out.println("34jh".matches("[\\w&&[^_]]{4}")); // true
    }
}

4 正则表达式应用

4.1 正则表达式常见应用案例


😎 需求

  • 请编写程序模拟用户输入手机号码、验证格式正确,并给出提示,直到格式输入正确为止。
  • 请编写程序模拟用户输入邮箱号码、验证格式正确,并给出提示,直到格式输入正确为止。
  • 请编写程序模拟用户输入电话号码、验证格式正确,并给出提示,直到格式输入正确为止。

😵 分析

  • 定义方法,接收用户输入的数据,使用正则表达式完成检验,并给出提示。

😇 示例代码:

import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        checkPhone();
        checkEmail();
        checkTel();
    }
    public static void checkPhone(){
        Scanner sc =new Scanner(System.in);
        while (true) {
            System.out.println("请您输入你的注册手机号码:");
            String phone = sc.next();
            if (phone.matches("1[3-9]\\d{9}")){
                System.out.println("手机号码格式正确,注册成功!");
                break;
            }else{
                System.out.println("手机号码格式有误!");
            }
        }
    }
    public static void checkEmail(){
        Scanner sc =new Scanner(System.in);
        while (true) {
            System.out.println("请您输入你的邮箱:");
            String phone = sc.next();
            // 123@qq.com
            // 23ggyfgy@163.com
            // 331122weiwei@pci.com.cn
            // 这里注意.的正则表达式
            if (phone.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]){1,2}")){
                System.out.println("邮箱格式正确,注册成功!");
                break;
            }else{
                System.out.println("邮箱格式有误!");
            }
        }
    }
    public static void checkTel(){
        Scanner sc =new Scanner(System.in);
        while (true) {
            System.out.println("请您输入你的电话号码:");
            String phone = sc.next();
            if (phone.matches("0\\d{2,6}-?\\d{5,20}")){
                System.out.println("电话号码格式正确,注册成功!");
                break;
            }else{
                System.out.println("电话号码格式有误!");
            }
        }
    }
}

4.2 正则表达式在字符串方法中的使用


在这里插入图片描述
🙋举个栗子:

public class Demo {
    public static void main(String[] args) {
        String names = "张三huuigh7566756李四vtftr65675王五tftyftyfty999赵六";
        String[] arrs = names.split("\\w+");
        for (int i = 0; i < arrs.length; i++) {
            System.out.println(arrs[i]);
        }
        /*
        /张三
        李四
        王五
        赵六
         */
        String s = names.replaceAll("\\w+", "\\\\");
        System.out.println(s); // 张三\李四\王五\赵六
        
        String s1 = names.replaceAll("\\w+", "\t");
        System.out.println(s1); // 张三	李四	王五	赵六

        String s2 = names.replaceAll("\\w+", "\\t"); // 同直接替换成t
        System.out.println(s2); // 张三t李四t王五t赵六
    }
}

4.3 正则表达式爬取信息


正则表达式支持爬取指定的信息

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

public class Demo {
    public static void main(String[] args) {
        String rs = "来学习Java,电话020-43422424,或者联系邮箱" +
                "ibeit@ibeit.cn,电话18762832533,0203232323" +
                "邮箱bozai@itcast.cn,400-150-3233 ,4001073232";
        // 需求:从上面的内容中爬取出 电话号码和邮箱
        // 1.定义爬取规则
        String regex = "(\\w{1,}@\\w{2,10}(\\.\\w{2,10}){1,2})|" +
                "(1[3-9]\\d{9})|(0\\d{2,5}-?\\d{5,15})|400-?\\d{3,8}-?\\d{3,8}";
        // 2.编译正则表达式成为一个匹配规则对象
        Pattern pattern = Pattern.compile(regex);
        // 3.通过匹配规则对象得到一个匹配数据内容的匹配器对象
        Matcher matcher = pattern.matcher(rs);
        // 4.通过匹配器去内容中爬取出信息
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
        /*
        020-43422424
        ibeit@ibeit.cn
        18762832533
        0203232323
        bozai@itcast.cn
        400-150-3233
        4001073232
        * */
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值