正则表达式学习笔记

1.字符

1) 在某个范围之内[ ]

      1>直接将所有内容列出来

        Pattern pattern = Pattern.compile("[asd]c"); 
        Matcher matcher = pattern.matcher("ac");
        System.out.println(matcher.find());
        matcher = pattern.matcher("gc");
        System.out.println(matcher.find());
          结果为:

       true
       false
      2>如果内容有规律,可以使用“-”来表示范围
        Pattern pattern = Pattern.compile("[a-z]m"); 
        Matcher matcher = pattern.matcher("am");
        System.out.println(matcher.find());
        pattern = Pattern.compile("[1-8]c"); 
        matcher = pattern.matcher("3c");
        System.out.println(matcher.find());
          结果为:

       true
       true
2) 取不在某个范围,即非,使用^
       Pattern pattern = Pattern.compile("[^asd]c"); 
       Matcher matcher = pattern.matcher("gc");
       System.out.println(matcher.find());
       matcher = pattern.matcher("ac");
       System.out.println(matcher.find());

          结果为:

       true
       false
3)取在范围1,或者范围2,或者范围3中,即或,直接不写就是

        Pattern pattern = Pattern.compile("[a-z1-5A-X]c"); 
	Matcher matcher = pattern.matcher("gc");
        System.out.println(matcher.find());
	matcher = pattern.matcher("3c");
        System.out.println(matcher.find());
         结果为:

     true
     true
       注:还有另外一种写法[a-z[1-5[A-Z]]]c效果相同
4)  取交集,使用&&

        String s1 = "ac";
        String s2 = "cc";
        System.out.println(s1.matches("[a-z&&[b-e]]c"));
        System.out.println(s2.matches("[a-z&&[b-e]]c"));
         结果为:

       false
       true

2.  预定义字符类

       产生原因:如果一直使用[a-z1-5]这样的正则表达式,会使阅读性降低,于是就使用几个专门的表达式用来表达一些特定的意思,如\d就表示0-9数字字符,等价于[0-9]
                                          

          String s1 = "2";
          String s2 = "c";
          System.out.println(s1.matches("\\d"));
          System.out.println(s2.matches("\\w"));
           结果为:

         true
         true

3.量词

          1)作用:就是为了说明字符或者字符串出现的次数

          2)x?  匹配0次或1次(?表示是否,所以为0,1)

        String str = "a";
        System.out.println(str.matches("a?"));
              结果为:true

         3) x+    匹配1次或多次(+表示正,所以为1或多次)

         String str = "a";
         System.out.println(str.matches("a+"));
                结果为:true
         4) x*   匹配0次或多次(*表示所有,所以几次都行)

         String str = "a";
         System.out.println(str.matches("a*"));
                结果为true

         5) x{n}  匹配n次

        String str = "aaa";
        System.out.println(str.matches("a{3}"));
                结果为true

         6)x{n,} 匹配大于n次(联想[n,],相当于从n到正无穷)

        String str = "aaa";
        System.out.println(str.matches("a{2,}"));
         7)x{n,m} 匹配大于n,小于m次(联想[n,m])
        String str = "aaa";
        System.out.println(str.matches("a{2,3}"));
         8)注意:

              1> [abc]+表示的是a一次或多次,或者b一次或多次,或者c一次或多次

         String str = "a";
         System.out.println(str.matches("[abc]+"));
               2> abc+表示的是只针对c匹配一次或多次
         String str = "abcc";
         System.out.println(str.matches("abc+"));
                 结果为true
               3>( abc)+表示的是针对整个abc匹配一次或多次
         String str = "abcabc";
         System.out.println(str.matches("(abc)+"));
                 结果为true
               4>类似的,x{n},x{n,m},x{n,}也是如此匹配的

4.元字符

          1)也就是有特殊意义的字符

          2)所有元字符:([{\^-$|}])?*+.
          3)将元字符改为普通意义的字符:前面加上\

5.边界判断

          ^行首        $行尾
         String str = "aaaa";
         System.out.println(str.matches("^a{2,}$"));
                  结果为true
         String str = "      aaaa";
         System.out.println(str.matches("^a{2,}$"));
                  结果为false
         String str = "aaaa  ";
         System.out.println(str.matches("^a{2,}$"));
                  结果为false

7.使用

            从一大段字符串中匹配需要的
       String str = "345345ABC23423456ASD123";
       Pattern p=Pattern.compile("[A-Z]{3}",Pattern.CASE_INSENSITIVE);
       Matcher m = p.matcher(str);
       while(m.find())
       {
          System.out.println(m.group());
       }
          结果为:
ABC
ASD
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值