@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)表示正則中第二個()中表示的正則匹配值
以此類推
