Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

Named capture groups

Reference for named capturing groups in Java regex: the (?<name>...) syntax, group(String), \k<name> backreferences, ${name} in replacements and namedGroups().

A named capturing group gives a parenthesized part of a regex a label: (?<year>\d{4}). You then read it with matcher.group("year") instead of counting parentheses. Named groups are still numbered as well, in the same left-to-right order as ordinary groups, so both access styles work.

Names make patterns self-documenting and survive edits: inserting a new group in the middle shifts all later numbers but leaves the names alone. A name must start with an ASCII letter and may then contain ASCII letters and digits (no underscore, no hyphen, no space).

Java
Pattern p = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})");
Matcher m = p.matcher("2026-03");
if (m.matches()) { System.out.println(m.group("month")); }   // 03

Try it

Java Reading groups by name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Backreferences and replacements

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
(?<name>subpattern)       // definition
\k<name>                  // backreference inside the pattern
${name}                   // reference in a replacement string
matcher.group("name")     // read the captured text
matcher.start("name"), matcher.end("name")
pattern.namedGroups()     // Map<String,Integer> (Java 20)
Part Description
name Starts with a letter a-z or A-Z, followed by letters or digits. Must be unique within the pattern.
group(String) Returns the text, or null if the group did not participate.
start(String), end(String) Offsets of the group, or -1.
MatchResult.namedGroups() Map of names to group numbers on Matcher and MatchResult (Java 20).

Throws: IllegalArgumentException from group(String) if no group has that name; IllegalStateException if there is no current match; PatternSyntaxException when a name is invalid or duplicated.

Usage notes

  • Still numbered. A named group also has a number, so group(2) works. Numbering counts every capturing group, named or not, in order of opening parenthesis.
  • Use (?:...) for grouping that should not capture at all.
  • Underscores are not allowed in names, unlike in Python ((?P<first_name>...)) or .NET. Use firstName.
  • No duplicate names. Defining the same name twice is a syntax error in Java.
  • Records pair well. Extract several named groups into a record in one small method; it reads better than a row of group(1), group(2).
  • In replacements write ${name}. A plain $name is not recognized, and a literal dollar sign needs a backslash (\\$) or Matcher.quoteReplacement.
  • Before Java 20, you could read a group by name but could not list the names. namedGroups() was added to Pattern and Matcher.