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