How to Use Regex Groups in Java: Simple Guide with Examples
In Java, you use regex groups by placing parts of your pattern inside parentheses
(). You can then access these groups using the Matcher.group() method after matching the pattern with Pattern and Matcher classes.Syntax
Regex groups are created by enclosing a part of the pattern in parentheses (). Each group is numbered starting from 1 based on the order of opening parentheses. Group 0 always refers to the entire match.
Pattern.compile("(pattern)"): Compiles the regex with groups.Matcher matcher = pattern.matcher(input): Creates a matcher for the input string.matcher.find(): Finds the next match.matcher.group(n): Returns the text matched by group numbern.
java
import java.util.regex.*; Pattern pattern = Pattern.compile("(group1)(group2)"); Matcher matcher = pattern.matcher("input string"); if (matcher.find()) { String wholeMatch = matcher.group(0); // entire match String firstGroup = matcher.group(1); // first group String secondGroup = matcher.group(2); // second group }
Example
This example shows how to extract the area code and phone number from a phone string using regex groups.
java
import java.util.regex.*; public class RegexGroupsExample { public static void main(String[] args) { String text = "My phone number is (123) 456-7890."; Pattern pattern = Pattern.compile("\\((\\d{3})\\) (\\d{3}-\\d{4})"); Matcher matcher = pattern.matcher(text); if (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); System.out.println("Area code: " + matcher.group(1)); System.out.println("Phone number: " + matcher.group(2)); } else { System.out.println("No match found."); } } }
Output
Full match: (123) 456-7890
Area code: 123
Phone number: 456-7890
Common Pitfalls
Common mistakes when using regex groups in Java include:
- Forgetting to escape special characters like parentheses in the regex pattern.
- Using
matcher.group()before callingmatcher.find()ormatcher.matches(). - Confusing group 0 (whole match) with group 1 (first group).
- Not checking if a match exists before accessing groups, which causes exceptions.
java
import java.util.regex.*; public class RegexPitfall { public static void main(String[] args) { String text = "abc123"; Pattern pattern = Pattern.compile("(\\d+)"); // correct: digits group Matcher matcher = pattern.matcher(text); // Wrong: accessing group before find() // System.out.println(matcher.group(1)); // Throws IllegalStateException if (matcher.find()) { System.out.println("Digits found: " + matcher.group(1)); } } }
Output
Digits found: 123
Quick Reference
| Concept | Description |
|---|---|
| Group syntax | Use parentheses () to create groups in regex. |
| Group numbering | Group 0 is the whole match; groups start at 1. |
| Access groups | Use matcher.group(n) after matcher.find() or matcher.matches(). |
| Escape special chars | Escape parentheses and other special chars with double backslash \\. |
| Check match | Always check matcher.find() before accessing groups. |
Key Takeaways
Use parentheses () in regex to create groups and capture parts of the match.
Always call matcher.find() or matcher.matches() before accessing groups with matcher.group(n).
Group 0 returns the entire matched text; groups start numbering from 1.
Escape special regex characters like parentheses with double backslashes in Java strings.
Check if a match exists to avoid exceptions when accessing groups.