Java复习总结之正则表达式

引言

正则图形化界面:https://regexper.com/

语法

1、\
  • 在其他语言中,\\ 表示:我想要在正则表达式中插入一个普通的(字面上的)反斜杠,请不要给它任何特殊的意义。
  • 在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。

所以,在其他的语言中,一个反斜杠 \ 就足以具有转义的作用,而在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。

也可以简单的理解在 Java 的正则表达式中,两个 \\代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\。

		System.out.println("5".matches("\\d"));
2、^和$

^:匹配输入字符串开始的位置。
$:匹配输入字符串结束的位置。

        String regex = "^abc$";
        System.out.println("ac".matches(regex));
        System.out.println("abc".matches(regex));
3、* 和+和?

* :零次或多次匹配前面的字符或子表达式。
+:一次或多次匹配前面的字符或子表达式。
?:零次或一次匹配前面的字符或子表达式。
注:前面的字符表示的是这个符号的前一个字符

        String regex1 = "12*";
        System.out.println("1".matches(regex1));
        System.out.println("222".matches(regex1));
        System.out.println("1222".matches(regex1));
        String regex2 = "12+";
        System.out.println("1".matches(regex2));
        System.out.println("12".matches(regex2));
        System.out.println("1222".matches(regex2));
        String regex3 = "12?";
        System.out.println("1".matches(regex3));
        System.out.println("12".matches(regex3));
        System.out.println("1222".matches(regex3));
4、{}

{n}:n 是非负整数。正好匹配 n 次。
{n,}:n 是非负整数。至少匹配 n 次。
{n,m}:m 和 n 是非负整数,其中 n <= m。匹配至少 n 次,至多 m 次。
注:不能将空格插入逗号和数字之间。

        String regex4 = "12{2,4}";
        System.out.println("1".matches(regex4));
        System.out.println("12".matches(regex4));
        System.out.println("1222".matches(regex4));
        System.out.println("122222".matches(regex4));
5、贪婪匹配和非贪婪匹配

正则表达式默认使用贪婪匹配
当?字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。
在这里插入图片描述

那么,什么是贪婪匹配和非贪婪匹配呢?

  • 贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。
  • 非贪婪匹配:就是匹配到结果就好,就少的匹配字符。
    public static void main(String[] args) {
        String result="{\"code\":200;\"message\":\"success\";\"data\":\"null\"}";
        //贪婪模式
        String rule1=".+code\".+";
        Pattern p1 =Pattern.compile(rule1);
        Matcher m1 = p1.matcher(result);
        while(m1.find()){
            System.out.println("匹配结果:"+m1.group(0));
        }
        //非贪婪模式
        String rule2=".+code\".+?";
        Pattern p2 =Pattern.compile(rule2);
        Matcher m2 = p2.matcher(result);
        while(m2.find()){
            System.out.println("匹配结果:"+m2.group(0));
        }
    }

在这里插入图片描述

6、.

匹配除"\r \n"之外的任何单个字符

        String regex7 = "1.*";
        System.out.println("12".matches(regex7));
        System.out.println("1212".matches(regex7));
        System.out.println("1lx".matches(regex7));
        String regex8 = "1.?";
        System.out.println("12".matches(regex8));
        System.out.println("1ll".matches(regex8));
        System.out.println("1".matches(regex8));
        String regex9 = "1.+";
        System.out.println("12".matches(regex9));
        System.out.println("1ll".matches(regex9));
        System.out.println("1".matches(regex9));
7、字符匹配

\d:匹配单个数字,数字0-9
\D:匹配单个非数字,a、-、“等
\w:匹配单个字母、数字、下划线
\W:匹配单个非字母、非数字、非下划线,如{、)、]
\s:匹配单个空白字符,如空格、回车、制表符
\S:匹配单个非空白字符

        String regex10 = "123\\dabcd\\wolx\\s";
        System.out.println("1230abcd7olx\n".matches(regex10));
        System.out.println("123zabcd_olx\n".matches(regex10));
        System.out.println("1230abcdColx\t".matches(regex10));
        String regex11 = "123\\Dabcd\\Wolx\\S";
        System.out.println("123tabcd]olxs".matches(regex11));
        System.out.println("123zabcd_olxs".matches(regex11));
        System.out.println("1230abcdColxs".matches(regex11));
8、范围匹配【[]、()】

[abc]:字符集。匹配包含的任一字符
[a-z]: 匹配所有的小写字母
[A-Z] :匹配所有的大写字母
[a-zA-Z] : 匹配所有的字母
[D-f]:匹配D-Z,a-f之间任意一个字母,不能写为[f-D],因为大写字母的Ascii码排在小写字母前
[0-9]:匹配所有的数字
[3-6]:匹配3-6之间任意一个数字
(abc):abc作为一个整体进行匹配
[^xyz]:反向字符集。匹配未包含的任何字符。
[^a-z]:反向范围字符。匹配不在指定的范围内的任何字符。

        String regex12 = "123[abc]021[a-zA-Z]bh_[C-c]_(abc)+";
        System.out.println("123a021obh_Z_abcabc".matches(regex12));
        System.out.println("123d021dbh_f".matches(regex12));
        System.out.println("123a021_bh_a".matches(regex12));
        System.out.println("123a021obh_Z_".matches(regex12));
        String regex13 = "123[^abc]021_[^a-z]";
        System.out.println("123a021_A".matches(regex13));
        System.out.println("123d021_A".matches(regex13));
        System.out.println("123d021_c".matches(regex13));
9、边界匹配

\b:匹配一个单词边界,也就是单词和空格之间的位置,不匹配任何字符;
\B: \b取非,即匹配一个非单词边界;

		String str=",,,,呵呵,,,,";
		String rex="\\b呵呵\\b";
        Pattern pattern=Pattern.compile(rex);
        Matcher matcher=pattern.matcher(str);
        if(matcher.find()){
            System.out.println(matcher.group());
        }else {
            System.out.println("no found!!!");
        }
        String str="123456我是JAVA{,、;‘asd";
        String rex="\\B";
        Pattern pattern=Pattern.compile(rex);
        String [] result=pattern.split(str);
        for(String string:result){
            System.out.println("分割的字符串:"+string);
        }
10、分支匹配

x|y:匹配 x 或 y

		String regex14 = "propert(y|ies)";
        System.out.println("property".matches(regex14));
        System.out.println("properties".matches(regex14));

附:参考资料

1、greedy、reluctant和possessive量词的区别
2、Java Pattern类的用法详解(正则表达式)
3、Java 正则表达式
4、正则表达式
5、超详细的正则表达式的使用方法,学不会找我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

--流星。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值