java 正则表达式获取值


   @Test    
  public void testtest() {
       String test = "hahahhehe sendCode\":\"12367890123rsdfsdfsdfdsahahhehe sendCode\":\"12367890123rsdfsdfsdfds";
       test = PropsUtil.regMatch(test, "sendCode\":\"([\\d]{8})([\\d]{3})");      

   }

    public static String regMatch(String withinText, String regString) {
        String code = null;
        Pattern pattern = Pattern.compile(regString);
        Matcher matcher = pattern.matcher(withinText);
        if (matcher.find()) {
            matcher.reset();
            while (matcher.find()) {
                code = matcher.group(1);
                System.err.println("aaaa" + code);
                code = matcher.group(0);
                System.err.println("bbbb" + code);
                code = matcher.group(2);
                System.err.println("ccc" + code);
            }
        }
        return code;
    }
}

运行结果

aaaa12367890
bbbbsendCode":"12367890123
bbbb123
aaaa12367890
bbbbsendCode":"12367890123
bbbb123

匹配后group(0)表示整个匹配的串

group(1)表示正则中第一个()中表示的正则匹配值

group(2)表示正则中第二个()中表示的正则匹配值

以此类推

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM