Captures and named groups

Capture groups

Parentheses in a pattern do two things: they group for quantifiers, and they capture the text that matched. Each pair lands in a numbered variable, starting at $1.

my $date = "2026-10-27"; if ($date =~ /^(\d{4})-(\d{2})-(\d{2})$/) { print "year $1, month $2, day $3\n"; }

perl demo.pl

year 2026, month 10, day 27

The numbered variables are only reliable right after a successful match. Another pattern anywhere in between overwrites them, which is the most common bug in code that parses several fields.

Captures as a list

In list context a successful match returns the captures directly, which avoids the numbered variables entirely. Assign the result and check it with defined.

my $date = "2026-10-27"; my ($y, $m, $d) = $date =~ /^(\d{4})-(\d{2})-(\d{2})$/; print "$y/$m/$d\n" if defined $d; # 2026/10/27

perl demo.pl

2026/10/27

A failed match returns an empty list, so the left side variables come out undef. That behavior makes the defined check the natural companion of this style.

Named captures

Numbers get unreadable past three groups. Name each one with (?<name>...) and read it from the %+ hash, where the keys are the names.

my $log = "2026-10-27 ERROR disk full"; if ($log =~ /^(?<date>\S+) (?<level>\w+) (?<msg>.+)$/) { print "$+{date} [$+{level}] $+{msg}\n"; }

perl demo.pl

2026-10-27 [ERROR] disk full

The same names work in a replacement string, and they keep the pattern self-documenting when a colleague has to change it six months later.

Optional groups

A group followed by a question mark may be missing. It still captures when present, and the variable is undef when absent, which lets one pattern accept several input shapes.

my @lines = ("warning: low", "low"); for my $l (@lines) { my ($lvl, $msg) = $l =~ /^(?:(\w+):\s*)?(.+)$/; $lvl //= "info"; print "$lvl - $msg\n"; }

perl demo.pl

warning - low
info - low

The (?:...) form groups without capturing, so the counter for numbered groups stays predictable. Use it for every group you only need for a quantifier.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What do parentheses do in a pattern?

    Answer

    They group for a quantifier and capture the matched text into a numbered variable

    The first pair goes into $1, the second into $2, and so on, which is how fields are read out of a match.

  2. When are $1, $2 and $3 safe to read?

    Answer

    Only right after a successful match: another pattern in between overwrites them

    The numbered variables belong to the last successful match, which is why copying them into my variables at once is the habit.

  3. What does a failed match give a list assignment such as my ($y, $m, $d) = $date =~ /.../;?

    Answer

    An empty list, so the variables come out undef

    A failed match returns no captures at all, which is why the defined $d check goes with this style.

  4. Where do the values of (?<level>\w+) land?

    Answer

    In the %+ hash under the key level

    A named capture is read as $+{level}, which keeps a long pattern readable and the code self-documenting.

  5. What does a group followed by ? capture when the group is absent from the text?

    Answer

    Nothing: the variable is undef, which a default fixes

    The optional group simply does not participate, so its capture is undef and //= can supply a default.

  6. Why write (?:...) instead of (...) around a piece you only want to quantify?

    Answer

    It groups without capturing, so the numbering of the real captures stays predictable

    A non-capturing group keeps the numbers small, so $1 still means the field you care about.

  7. What does this program print? my $d = "2026-10-27"; my ($y, $m, $day) = $d =~ /^(\d{4})-(\d{2})-(\d{2})$/; print "$y/$m/$day\n";

    Answer

    2026/10/27

    The three captures arrive in the order of their groups and the print joins them with slashes.

  8. What does this program print? my $log = "2026-10-27 ERROR disk full"; if ($log =~ /^(?<date>\S+) (?<level>\w+) (?<msg>.+)$/) { print "$+{level}: $+{msg}\n"; }

    Answer

    ERROR: disk full

    The level capture is ERROR and the message capture takes the rest of the line.

  9. What does this program print? my $s = "abc"; my ($a, $b) = $s =~ /(x)(y)/; print defined $b ? "matched" : "no match", "\n";

    Answer

    no match

    The pattern cannot match, so both captures are undef and the ternary takes the second branch.

  10. What does this program print? my @l = ("warning: low", "low"); for my $l (@l) { my ($lvl, $msg) = $l =~ /^(?:(\w+):\s*)?(.+)$/; $lvl //= "info"; print "$lvl - $msg\n"; }

    Answer

    warning - low / info - low

    The first line has the optional level and keeps it; the second has none, so the default of info applies.

Article author: Arthur Isaev