import java.util.regex.*; import java.io.*; public class CaptureTextInGroup{ public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter text for finding groups of text: "); String inputStr = in.readLine(); String patternStr = "(a(b*))+(c*)"; // Compile and use regular expression Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); if(matchFound){ // Get all groups for this match for(int i=0; i<=matcher.groupCount(); i++){ String groupStr = matcher.group(i); System.out.println(groupStr); } } } }