加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

强调在正则中只要单纯用了括号就会有捕获分组保存

发布时间:2020-12-14 00:44:55 所属栏目:百科 来源:网络整理
导读:转载于:http://www.jb51.cc/article/p-ojixyucg-bqm.html 前言: 本篇文章主要为了强调正则中1的引用,1为括号的编号。和不加不必要的括号,以防影响效率。 import java .util .regex .Matcher ; import java .util .regex .Pattern ; public class li14 {

转载于:http://www.52php.cn/article/p-ojixyucg-bqm.html

前言:

本篇文章主要为了强调正则中1的引用,1为括号的编号。和不加不必要的括号,以防影响效率。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li14 {
        public static void main(String[] args) {
             String string="hello 123 hello";
             String rex="(w+)s+(d+)s+(1)";

             Pattern pattern=Pattern.compile(rex);
             Matcher matcher=pattern.matcher(string);

             if(matcher.find()){
                 System.out.println("匹配成功");
                 System.out.println("匹配编号0的匹配字符:["+matcher.group(0)+"]");
                 System.out.println("匹配编号1的匹配字符:["+matcher.group(1)+"]");
                 System.out.println("匹配编号2的匹配字符:["+matcher.group(2)+"]");
                 System.out.println("匹配编号3的匹配字符:["+matcher.group(3)+"]");
             }
        }
}
匹配编号0的匹配字符:[hello 123 hello]
匹配编号1的匹配字符:[hello]
匹配编号2的匹配字符:[123]
匹配编号3的匹配字符:[hello]

例子2

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li14 {
        public static void main(String[] args) {
             String string="hello 123 hello";
             String rex="(w+)s+(d+)s+1";

             Pattern pattern=Pattern.compile(rex);
             Matcher matcher=pattern.matcher(string);

             if(matcher.find()){
                 System.out.println("匹配成功");
                 System.out.println("匹配编号0的匹配字符:["+matcher.group(0)+"]");
                 System.out.println("匹配编号1的匹配字符:["+matcher.group(1)+"]");
                 System.out.println("匹配编号2的匹配字符:["+matcher.group(2)+"]");
                // System.out.println("匹配编号3的匹配字符:["+matcher.group(3)+"]"); //如果把编号3放开就会报越界异常
             }
        }
}

运行结果:

匹配成功
匹配编号0的匹配字符:[hello 123 hello]
匹配编号1的匹配字符:[hello]
匹配编号2的匹配字符:[123]

在上面这两个例子中,例子1的”//1”外面多加了括号所以,就多了捕获分组,并且保存了捕获分组结果。
然而加这个括号往往是多余的。因为//1已经可以正确表示第一个捕获分组的结果了。就不需要再多加个括号。

然而想要多加个括号也可以:
看例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class li14 {
        public static void main(String[] args) {
             String string="hello 123 hello";
             String rex="(w+)s+(d+)s+(?:1)";

             Pattern pattern=Pattern.compile(rex);
             Matcher matcher=pattern.matcher(string);

             if(matcher.find()){
                 System.out.println("匹配成功");
                 System.out.println("匹配编号0的匹配字符:["+matcher.group(0)+"]");
                 System.out.println("匹配编号1的匹配字符:["+matcher.group(1)+"]");
                 System.out.println("匹配编号2的匹配字符:["+matcher.group(2)+"]");
                // System.out.println("匹配编号3的匹配字符:["+matcher.group(3)+"]");//同样如果把编号3放开就会报越界异常
             }
        }
}

运行结果:

匹配成功
匹配编号0的匹配字符:[hello 123 hello]
匹配编号1的匹配字符:[hello]
匹配编号2的匹配字符:[123]

总结,不用的括号一定要去掉,否者影响正则的匹配效率,因为括号会保存捕获分组,这样不仅耗费内存也耗费时间。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读