zoukankan      html  css  js  c++  java
  • Java正则表达式中的捕获组的概念及相关API使用

    要弄清这三个方法,首先要弄清Java正则表达式中的捕获组的概念。捕获组也就是Pattern中以括号对“()”分割出的子Pattern。至于为什么要用捕获组呢,主要是为了能找出在一次匹配中你更关心的部分。
    捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 "(x)(y\w*)(z)" 中,存在三个这样的组: 
    1.  x
    2.  y\w*
    3.  z
    始终代表整个表达式。
    之所以这样命名捕获组是因为在匹配中,保存了与这些组匹配的输入序列的每个子序列。捕获的子序列稍后可以通过 Back 引用在表达式中使用,也可以在匹配操作完成后从匹配器获取。
    以 (?) 开头的组是纯的非捕获 组,它不捕获文本,也不针对组合计进行计数。

    Example:

    package pattern;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class testRegex {
        public static void main(String[] args) {
            String regex = "(x)(y\w*)(z)";
    
            String input = "exy123z,xy456z";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(input);
    
            while (m.find()) {
                System.out.println(m.group(2));
            }
        }
    }
    

      

    运行结果:
    y123
    y456

    http://blog.163.com/xiejunshlh@126/blog/static/1662603142011219625597/

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /*2015-9-9*/
    public class RegexDemo {
        public static void main(String[] args) {
            String regex = "(T_V)([\d])|(xx)+";
            String source = "T_V123,TX_V1,T_V234";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(source);
            System.out.println("是否匹配:"+matcher.matches());
            if (matcher.find()) {
                for (int i = 0; i <= matcher.groupCount(); i++) {
                    String group = matcher.group(i);
                    System.out.println("number " + i + ":" + group+"; Is null:"+(group==null));
                }
            }
        }
    
    }

    输出:

    是否匹配:false
    number 0:T_V1; Is null:false
    number 1:T_V; Is null:false
    number 2:1; Is null:false
    number 3:null; Is null:true

    解析:
    (1)Matcher.matches()返回值为false,是因为matches是正则表达式和整个字符串进行匹配
    API:

    public boolean matches()
        Attempts to match the entire region against the pattern.
        If the match succeeds then more information can be obtained via the start, end, and group methods.
        Returns:
            true if, and only if, the entire region sequence matches this matcher's pattern
    
    注:
    matches
    public boolean matches()
        尝试将整个区域与模式匹配。
        如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
        返回:
            当且仅当整个区域序列匹配此匹配器的模式时才返回 true。

    (2)Matcher.find() 源字符串中任一个子序列满足条件即返回true

    public boolean find()
        Attempts to find the next subsequence of the input sequence that matches the pattern.
        This method starts at the beginning of this matcher's region, or, 
    if a previous invocation of the method was successful and the matcher has not since been reset,
    at the first character not matched by the previous match. If the match succeeds then more information can be obtained via the start, end, and group methods. Returns: true if, and only if, a subsequence of the input sequence matches this matcher's pattern 注: find public boolean find() 尝试查找与该模式匹配的输入序列的下一个子序列。 此方法从匹配器区域的开头开始,如果该方法的前一次调用成功了并且从那时开始匹配器没有被重置,则从以前匹配操作没有匹配的第一个字符开始。 如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。 返回: 当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true。

    正则表达式匹配中文

    说明:
    (1)现在网上大多数用于判断中文字符的是 u4E00-u9FA5 这个范围是只是“中日韩统一表意文字”这个区间,但这不是全部,
    如果要全部包含,则还要他们的扩展集、部首、象形字、注间字母等等; 具体可以查看unicode中简体中文编码
    (2) "[一-龥]";是查出的u4E00-u9FA5对应的中文。具体uniocde2中文进行查询

    public class StringRegexDemo {
        public static void main(String[] args) {
            isMatch("唐", "[u4E00-u9FA5]");
            isMatch("晴", "[一-龥]");
        }
    
        protected static void isMatch(String source, String regexStr) {
            boolean result = source.matches(regexStr);
            System.out.println(result);
        }
    }
    

    输出:

    true
    true
    

    http://blog.csdn.net/xyls12345/article/details/23942533


     

  • 相关阅读:
    更好的抽屉效果(ios)
    系统拍照动画
    UITabBarController详解
    touch事件分发
    iOS UWebView详解
    iOS 监听声音按键
    webservice偶尔报黄页,解决方案
    FastReport脚本把数据绑定到文本控件上
    [转]js版的md5()
    JQuery中$.ajax()方法参数详解
  • 原文地址:https://www.cnblogs.com/softidea/p/3922002.html
Copyright © 2011-2022 走看看