Returning the String found with a regular expression

If I have a regular expression, how do I return the substring that it has found? I'm sure I must be missing something obvious, but I've found various methods to confirm that that substring is contained in the string I'm searching, or to replace it with something else, but not to return what I've found.

CharSequence inputStr = "abbabcd";

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 =;

A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.

CharSequence is an interface

public interface CharSequence

See Capturing groups

See group with parameter example

See Java Regex Tutorial

Matcher matcher = Pattern.compile("a+").matcher("bbbbaaaaabbbb");
     System.out.println(; //aaaaa

If you want specific parts

Matcher matcher = Pattern.compile("(a+)b*(c+)").matcher("bbbbaaaaabbbbccccbbb");
   System.out.println(; //aaaaa
   System.out.println(; //cccc
   System.out.println(; //aaaaabbbbcccc 

Group 0 is the complete pattern.. other groups are separated with parenthesis in the regex (a+)b*(c+) and can be get individually


While I do understand some of the corner-cases of generics, I'm missing something with the following example. I have the following class 1 public class Test<T> { 2 public static void main(...

My android app needs communicate with server when it's (application) opened. For that I want to use socketIO Java client. Do I need to use BoundService for sockets (it should run on background)? ...

I was poking around the rabbitmq documentation, and it seems that rabbitmq does not handle message redelivery count. If I were to manually ACK/NACK messages, I would need to either keep the retry ...

I'm not a Java guy but use Solr for searching, but after search about this issue I couldn't find out why it is happening. I have a 30-million-records-index with no sorting and the lightest setup I ...