正则表达式

8,372 阅读2分钟

概述

正则表达式是比较常见的格式校验方式。然而语法经常记不住, 这里做下笔记, 记录常用的正则表达式的用法。

案例

案例很简单, 无需过多文案, 直接上代码。

/**
 * @author 当我遇上你
 * @公众号 当我遇上你
 * @since 2020-07-09
 */
@Slf4j
public class RegExpTest {

    /**
     * 结果
     * 
     * 00:12:06.852 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^1234$], content=[1234], result=[true]
     * 00:12:06.860 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^1234|5678$], content=[1234], result=[true]
     * 00:12:06.861 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123[0-9]$], content=[1234], result=[true]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123\d$], content=[1234], result=[true]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123\d+$], content=[123456789], result=[true]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123\d*$], content=[123], result=[true]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123], content=[1235], result=[false]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[123$], content=[4123], result=[false]
     * 00:12:06.862 [main] INFO cn.idea360.demo.modules.regular.RegExpTest - 正则表达式: regex=[^123\d{5}], content=[12345678], result=[true]
     */
    public static void main(String[] args) {


        // "^" 匹配字符串的开头, "$" 匹配字符串的结尾, 所以本表达式严格匹配 1234
        String regex = "^1234$";
        String content = "1234";
        boolean matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // "|" 是或的意思, 表示匹配 1234 或 5678
        regex = "^1234|5678$";
        content = "1234";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // "[]" 表示匹配其中的任意一个字符, 其中 "-" 表示一个区间, 即 0 到 9, 它等于 [0123456789], 也就是说它会匹配 1230, 31, 1232, ..., 1239
        regex = "^123[0-9]$";
        content = "1234";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // 同上, \d 等于 [0-9]
        regex = "^123\\d$";
        content = "1234";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // "+" 号匹配一个或多个它前面的字符, 这里, 由于 "+" 前面是 "\d", 所以它就等于1个或多个数字, 实际上它匹配任意以 123 开头的至少4位数的数字串, 如 1230, 12300, 12311, 23456789等
        regex = "^123\\d+$";
        content = "123456789";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // "*" 号与 "+" 号的不同在于, 它匹配0个或多个前面的字符, 所以, 它匹配以 123 开头的至少3位数的数字串, 如 123, 123789
        regex = "^123\\d*$";
        content = "123";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // 跟上面一样, 由于没有结尾的 "$", 它匹配任意以 123 开头的数字串, 但除此之外, 它还匹配后面是字母的情况, 如123abc。(实测false, 不知道为啥)
        regex = "^123";
        content = "1235";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // 匹配任何以 123 结尾的字符串(实测false, 不知道为啥)
        regex = "123$";
        content = "4123";
        matches = content.matches(regex);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

        // {5} 表示精确匹配5位, 包含它前面的一个字符。在这里, 它匹配以 123 开头的所有8位的电话号码
        regex = "^123\\d{5}";
        content = "12345678";
        matches = Pattern.matches(regex, content);
        log.info("正则表达式: regex=[{}], content=[{}], result=[{}]", regex, content, matches);

    }
}

最后

本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。