正则表达式的使用

正则表达式的使用

  • 匹配规则(正则表达式的匹配为从左到右依次匹配)

    精准匹配

public class regex {
    public static void main(String[] args) {
            String reg="12345";
            System.out.println("12345".matches(reg)); print:true
          	  System.out.println("1234".matches(reg)); print:Flase
              
            //对于正则表达式12345 它只能匹配到12345 对于特殊字符 \,&| 需要使用\进行转义 \\,\& ,\|
            }
}

匹配任意字符

public class regex {
   public static void main(String[] args) {
        	   String reg="\\d";
	         System.out.println("1".matches(reg)); print:true
         	 System.out.println("r".matches(reg)); print:Flase
          // \d 表示匹配一个数字 \D表示匹配非数字
                
             reg="\w"+;
             System.out.println("1".matches(reg));\\ print:False
         	 System.out.println("r".matches(reg)); \\print:true
         	   // \d 表示匹配一个字母 \D表示匹配非字母
         	   
         	   //匹配多个字符
         	   reg="\w*"; //或者使用\w+
            	System.out.println("sss".matches(reg));\\ print:True
         	 //限定字符长度
         	 
         	  reg="\w{2}"; //在这里我们使用了{2}来限制有字符为2个
         	  //reg="\w{2,3}"; //在这里我们使用了{2,3}来限制有字符为2-3个
         	  //reg="\w{2,n}"; //在这里我们使用了{2,3}来限制有字符为2-n个
             System.out.println("ss".matches(reg));\\ print:True
             
           }
}
字母
.任意字符
\s空格或者tab键
  • 复杂匹配
    在正则表达式中使用 ^ 来表示开头 $ 表示 结尾
String reg="^y.*$n";
System.out.println("yuan".matches(reg)); //输出true
reg="^n";
System.out.println("yuan".matches(reg)); //输出true

使用 []来表示一个范围

String reg="[0-9]*"; //0-9表示匹配所有的数字 A-F 表示匹配所有大写字母
System.out.println("1234".matches(reg)); //输出true
reg="[j|u]";//表示匹配j或者u
System.out.println("j".matches(reg)); //输出true
System.out.println("u".matches(reg)); //输出true
  • 分组匹配
    在上面的介绍中我们只对字符进行了判断 ,当我们需要获取匹配的字符时 就需要使用到分组匹配 ,在正则表达式中分组匹配用()来表示
  Pattern p = Pattern.compile("(\\d{3})(-)(.*)");
        Matcher m = p.matcher("010-12345678");
        if (m.matches()) {
            String g1 = m.group(1);
            String g2 = m.group(2);
            String g3 = m.group(3);
            System.out.println(g1);
            System.out.println(g2);
        } else {
            System.out.println("匹配失败!");
        }
        //在上面代码中 将字符分为了3组 第一组取出前三个数字
        //第二组取出-
        //第三组取出 -后面所有数字

-贪婪模式与非贪婪模式
在正则表达式中默认开启贪婪模式
要使用非贪婪只需要在匹配符加上?
看下面这个列子 如何我们要匹配hhhh

 Pattern p1=Pattern.compile("(\\d+?)(.*)"); 
        Matcher M=p1.matcher("1234000");


        Pattern p = Pattern.compile("(\\w+?)(h*)");
        Matcher m = p.matcher("lisihhhhh");
        if (m.matches()) {
            String g1 = m.group(1); //输出lisi
            String g2 = m.group(2); //hhhhh
            System.out.println(g1);
            System.out.println(g2);
        } else {
            System.out.println("匹配失败!");
        }

在贪婪模式下 (\w+?)会匹配到更多的元素

在非贪婪模式下(\w*)会匹配到更多的元素

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值