1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.util.regex.Matcher; import java.util.regex.Pattern; publicclass TestRegular { publicstaticvoid main(String[] args) { String inputStr = "ABC\nABC\nABC"; String patternStr = "(?d)ABC"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); while(matchFound) { System.out.println(matcher.start() + "-" + matcher.end()); for(int i = 0; i <= matcher.groupCount(); i++) { String groupStr = matcher.group(i); System.out.println(i + ":" + groupStr); } if(matcher.end() + 1 <= inputStr.length()) { matchFound = matcher.find(matcher.end()); }else{ break; } } } } |