1
2import java.util.regex.Matcher;
3import java.util.regex.Pattern;
4
5public class Main {
6 public static void main(String[] argv) throws Exception {
7
8 CharSequence inputStr = "abbabcd";
9 String patternStr = "(a(b*))+(c*)";
10
11 Pattern pattern = Pattern.compile(patternStr);
12 Matcher matcher = pattern.matcher(inputStr);
13 boolean matchFound = matcher.find();
14
15 if (matchFound) {
16 // Get all groups for this match
17 for (int i = 0; i <= matcher.groupCount(); i++) {
18 // Get the group's captured text
19 String groupStr = matcher.group(i);
20
21 // Get the group's indices
22 int groupStart = matcher.start(i);
23 int groupEnd = matcher.end(i);
24
25 // groupStr is equivalent to
26 inputStr.subSequence(groupStart, groupEnd);
27 }
28 }
29 }
30}
31
32
33