Capturing Groups
Extract matched substrings.
Capturing Groups is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Capturing Groups Are
Parentheses ( ) in a regex create capturing groups. After a match you can extract each group's text separately, ideal for parsing structured data.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d+)-(\\d+)").matcher("42-99");
if (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
}Group 0 Is the Whole Match
group(0) (or group()) returns the entire matched text. Numbered groups start at 1.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d+)px").matcher("width: 24px");
if (m.find()) {
System.out.println(m.group(0));
System.out.println(m.group(1));
}
}
}Counting Groups
groupCount() reports the number of capturing groups in the pattern (not counting group 0).
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})").matcher("2026-05-30");
if (m.find()) System.out.println(m.groupCount());
}
}Parsing a Date
Capturing groups make it easy to break a formatted value into parts.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})").matcher("2026-05-30");
if (m.matches()) {
System.out.println("year=" + m.group(1));
System.out.println("month=" + m.group(2));
System.out.println("day=" + m.group(3));
}
}
}Named Groups
Name a group with (?<name>...) and retrieve it by name. This makes patterns far more readable.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Pattern p = Pattern.compile("(?<user>\\w+)@(?<host>\\w+\\.\\w+)");
Matcher m = p.matcher("alice@example.com");
if (m.find()) {
System.out.println(m.group("user"));
System.out.println(m.group("host"));
}
}
}Group Boundaries
start(n) and end(n) give the index range of a specific group within the input.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d+)").matcher("abc123");
if (m.find()) {
System.out.println(m.start(1) + " to " + m.end(1));
}
}
}Non-Capturing Groups
Use (?:...) to group for alternation or quantifiers without creating a numbered capture. This keeps group numbers meaningful.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(?:Mr|Ms)\\.? (\\w+)").matcher("Ms. Smith");
if (m.find()) System.out.println(m.group(1));
}
}Optional Groups May Be Null
If a group is optional and did not participate in the match, group(n) returns null. Always handle that case.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\d+)(px)?").matcher("50");
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
}Iterating Matches with Groups
Combine find in a loop with group extraction to pull structured tokens out of larger text.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\w+)=(\\d+)").matcher("a=1 b=2 c=3");
while (m.find()) {
System.out.println(m.group(1) + " -> " + m.group(2));
}
}
}Backreferences in the Pattern
Inside a regex, \1 matches the same text a prior group captured. This detects repetition, like a doubled word.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Matcher m = Pattern.compile("(\\w+) \\1").matcher("the the cat");
System.out.println(m.find());
System.out.println(m.group(1));
}
}Capturing into Records
A clean pattern: match, extract groups, and build a small object. Here we use a record.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Matcher m = Pattern.compile("\\((\\d+),(\\d+)\\)").matcher("(3,7)");
if (m.matches()) {
Point p = new Point(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)));
System.out.println(p);
}
}
}Quick Check
What does group(0) return after a successful match?
Recap
You extracted matched substrings:
- Parentheses create numbered groups;
group(0)is the whole match, 1+ are captures. (?<name>...)creates named groups read viagroup("name").(?:...)groups without capturing.- Optional groups can be
null;\1backreferences a prior group.
Frequently asked questions
Is the “Capturing Groups” lesson free?
Yes — the full text of “Capturing Groups” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Capturing Groups”?
Extract matched substrings. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Capturing Groups” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Compiling Patterns
- Finding and Matching
- Capturing Groups
- Replacing with Regex