Java中的正则表达式

参考文档:https://blog.youkuaiyun.com/j2974500224/article/details/140896185

一、正则表达式规则

1、字符内容匹配

[] : 表示匹配括号里的任意一个字符

 [abc]: 		匹配a、b、c 的任意一个字母;
 [a-z]:	 		匹配小写字母的任意一个字母;
 [^abc]: 		匹配 a、b、c 以外的任意一个字符; 
 [A-Za-z]: 		匹配所有的小写字母、大写字母的任意一个;
 [a-zA-Z0-9]: 	匹配所有的小写字母、大写字母、数字的任意一个; 
 [a-z&&[^bc]]: 	匹配除了b和c以外的所有的小写字母;

2、字符类型匹配

\d:	匹配数字字符中的任意一个,相当于[0-9]"\\d"表示通配符,"d"表示字符d
\w: 匹配单词字符(数字字母下划线)中的任意一个,相当于[a-zA-Z0-9_]"\\w"表示通配符,"w"表示字符w
\s:	匹配空格、制表符、退格符、换行符等中的任意一个。"\\s"表示通配符,"s"表示字符d
\D: 匹配非数字字符中的任意一个,相当于[^0-9]"\\D"表示通配符,"D"表示字符d
\W: 匹配非单词字符(非数字字母下划线)中的任意一个,相当于[^a-zA-Z0-9_]"\\W"表示通配符,"W"表示字符W
\S: 匹配非空格、制表符、退格符、换行符等中的任意一个 。"\\S"表示通配符,"S"表示字符S
. : 匹配除换行符(\n、\r)之外的任何单个字符,相当于[^\n\r]"."表示通配符,"\\."表示字符.

3、字符数量匹配

X ?   :  匹配0个或1X +   :  匹配1个及以上
X *   :  匹配任意多个(含0X {m} :  匹配m个
X {m,}:  匹配m个及以上
X {m,n}: 匹配m~n个

4、字符分组匹配

如:(153|156)[\\d]{8}   匹配开头是153156的号码

5、^和$

^:匹配字符串是否以指定表达式开头
如: ^[0-9]+    123abc456edf789 以123开头,满足 
$:	匹配字符串是否以指定表达式结尾
如: [0-9]+$    123abc456edf789 以789结尾,满足

二、正则表达式应用

1、String类自带了若干应用正则表达式的函数,如matches()、split()、replaceAll()等

        
        /**************************************
         *        0、字符串底层存储编码
         **************************************/
        byte[]  bytes = {-28, -72, -83, -27, -101, -67};
        String str1 = new String(bytes);
        System.out.println("str1 = " + str1);  // "中国"

        String str2 = new String(bytes,"UTF-8");
        System.out.println("str2 = " + str2);   // "中国"

        /**************************************
         *        1、正则表达式匹配字符串
         **************************************/
        System.out.println("a".matches("[abcdefg]"));   // true
        System.out.println("h".matches("[^abcdefg]"));  // true
        System.out.println("+".matches("."));           // 匹配+是不是一个字符 true
        System.out.println("-".matches("\\."));         // \\.通配符.变成了普通的点符号 false
        System.out.println("c".matches("\\w"));         //不添加[]也可以 true
        System.out.println("c".matches("[\\w]"));       //添加[] true
        // 匹配账号:账号必须是由字母数字、下划线组成的5~8位
        System.out.println("_abc123".matches("\\w{5,8}"));  //true
        System.out.println("_abc123+".matches("\\w{5,8}")); //false
        System.out.println("".matches("[a-z]?"));   		//true
        System.out.println("n".matches("[a-z]?"));  		//true
        System.out.println("1".matches("[a-z]?"));  		//false
        // 匹配密码:密码必须是8位的数字或字母组合
        System.out.println("abc12345".matches("[a-zA-Z0-9]{8}")); //true
        System.out.println("abc1234".matches("[a-zA-Z0-9]{8}"));  //false
        System.out.println("abc123456".matches("[a-zA-Z0-9]{8}"));//false
        // 匹配手机:137等开头的11位数字
        String regex = "(13|18|15)(7|8|9)[\\d]{8}";
        System.out.println("13811110000".matches(regex)); //true
        System.out.println("13311110000".matches(regex)); //false

        /**************************************
         *        2、正则表达式分隔字符串
         **************************************/
        /// 请使用数字将其切分成字符串数组。
        String text = "hello11world22smy333";
        String[] arr = text.split("\\d+");
        System.out.println(Arrays.toString(arr));
        text = "888hello99world10smy";
        arr = text.split("\\d+");
        System.out.println(Arrays.toString(arr));
        text = "123hello234world235smy";
        arr = text.split("3");
        System.out.println(Arrays.toString(arr));

        /**************************************
         *        3、正则表达式替换字符串
         **************************************/
        String url = "http://www.baidu.com.cn.456";
        //将所有的字母替换成#
        url = url.replaceAll("[a-zA-Z]+","#");
        System.out.println("url: " + url);

2、Strirng正则的底层是利用Pattern类和Matcher类进行字符串的匹配。因此,我们自己编码也可以直接使用这两个类进行字符串的匹配。

        /**
         * 匹配字符串的开头是否符合正则表达式
         */
        String text = "a123abc";
        String regex = "\\d+";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        boolean result  = matcher.lookingAt();
        System.out.println("匹配结果: " + result);
        
        /**
         * 匹配字符串是否符合正则表达式
         */
        String text = "smingyu12_";
        String regex = "[a-zA-Z][_$a-zA-Z0-9]{7,9}";
        Pattern pattern = Pattern.compile (regex);
        Matcher matcher = pattern.matcher(text);
        boolean result  = matcher.matches();
        System.out.println("匹配结果:" + result);

        /**
         * 匹配&提取字符串里符合正则表达式的子串
         */
        String text = "abc123eee444ff555ggg";
        String regex = "\\d+";
        List<String> target = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        int count = 0;
        while (matcher.find()) {
//            target.add(source.substring(matcher.start(), matcher.end()));
            target.add(matcher.group());
            count++;
        }
        System.out.println("匹配的子串数量:" + count);
        System.out.println(target);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值