Java Regex Capturing Groups
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Java Regular Expressions (Regex) are a powerful tool for pattern matching and data extraction in strings. Capturing groups are one of the core features that allow developers to extract specific segments of matched text. This article provides a detailed exploration of capturing groups in Java Regex, including technical explanations, examples, and some best practices.
Understanding Capturing Groups
Capturing groups in regex are denoted by parentheses (). They serve to:
- Define a subpattern, allowing you to extract parts of the matching string.
- Apply quantifiers to entire patterns.
- Create backreferences that can be used within regular expressions for repeated patterns.
Basic Syntax
In Java Regex, capturing groups are numbered by counting their opening parentheses from left to right, starting at 1. The full match is always group 0.
Example Pattern: (abc)
Output:
From this example, note that:
matcher.group(0)returns the entire match.matcher.group(1)andmatcher.group(2)return their respective groups.
Named Capturing Groups
Java 7 introduced named capturing groups, which give more clarity, especially in complex regex patterns. Named groups are defined with (?<name>...) syntax.
Example:
Output:
Non-Capturing Groups
Sometimes, you may want to group subpatterns without capturing them. This is achieved using (?:...). These groups are useful when you want to apply operators to a group of elements without saving the match.
Example: (a|b|c) can be written as (?:a|b|c) if you only need the alternation without capture.
Backreferences
Backreferences allow you to refer to captured groups later in the regex using \1, \2, etc., based on the group number. For named groups, you use \k<name>.
Example:
Output:
Summary Table
| Feature | Syntax | Description |
| Capturing Group | (pattern) | Saves the matched portion for later retrieval. |
| Named Capturing Group | (?<name>pattern) | Captures the match and assigns it a name for easier access. |
| Non-Capturing Group | (?:pattern) | Groups elements without capturing. |
| Backreference | \\n, \k<name> | Refers to a previously captured group by number or name. |
| Full Match Retrieval | matcher.group(0) | Returns the entire matched string. |
Practical Considerations
- Performance: Using excessively many capturing groups can affect performance and may complicate pattern logic.
- Complex Patterns: In complex regexes, prefer named groups for readability.
- Regex Testing: Always test your regex with various inputs to ensure they capture precisely what you expect.
Incorporating capturing groups into your Java applications can greatly enhance your ability to parse and manipulate strings efficiently. Understanding their syntax and application opens up a range of capabilities from data validation to intricate string manipulation.
Related reading
- Java RegEx meta character . and ordinary dot?
- Java ResultSet how to check if there are any results
- Java RMI adding numbers
- Java Round up Any Number
- Java Runtime.getRuntime getting output from executing a command line program
- Java SafeVarargs annotation, does a standard or best practice exist?
- Java SE 6 vs. JRE 1.6 vs. JDK 1.6 - What do these mean?
- Java Security Illegal key size or default parameters?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.