Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 43 ยท Regular Expressions

Captures and Modifiers

Pull data out of text with Perl regex capture groups, $1, named captures in %+, list-context matching, global matches with pos, and the /i /g /m /s /x modifiers.

A pattern that only says "yes, this matches" is useful, but most real jobs need the matched pieces: the year in a date, the user name in an address, every price in a paragraph. Perl's answer is capture groups. You wrap part of the pattern in parentheses, and Perl remembers what that part matched.

This lesson shows the three ways to get captured text out (the numbered variables, list context, and named captures), then the modifiers that change how a pattern behaves: /i, /g, /m, /s and /x. It builds on Regular Expressions Basics.

Perl is not installed on this site, so the Perl code below is read-only. The interactive examples are Python stand-ins: Python's re module has nearly the same pattern syntax, so you can experiment with the same ideas. Differences are called out in the text.

Capture groups and $1

Every pair of plain parentheses in a pattern is a capture group. After a successful match, the text matched by the first group is in $1, the second in $2, and so on. Groups are numbered by the position of their opening parenthesis, counting from the left, so nested groups are easy to follow.

PERL
use v5.36;

my $date = "2026-08-03";
if ($date =~ /^(\d{4})-(\d\d)-(\d\d)$/) {
    say "year=$1 month=$2 day=$3";
}
TEXT
year=2026 month=08 day=03

Two rules keep you out of trouble:

  • Use $1 only after a successful match. If a match fails, Perl does not clear the old values, so $1 still holds the text from the last match that worked. Always put the use of $1 inside the if.
  • Copy captures into your own variables quickly. Any later successful match in the same scope replaces $1, $2 and the rest.
PERL
"abc" =~ /(b)/;
"xyz" =~ /(q)/;      # fails, $1 is untouched
say $1;              # prints: b

If you want grouping without capturing (for alternation or a quantifier), use (?:...). It does not take a number, so your numbered variables stay tidy:

PERL
if ("report.pdf" =~ /^(\w+)\.(?:pdf|txt)$/) {
    say $1;          # report
}

Three ways to read a capture

Numbered $1, $2, $3 after a successful match.
List context Assign the match to a list of variables.
Named %+ gives each group a readable name.

Pick named captures when a pattern has more than two groups.

1 / 2

Capturing in list context

A match in list context returns the captures as a list, with no $1 needed. This is the tidiest way to unpack a pattern into named variables:

PERL
my ($year, $month, $day) = $date =~ /^(\d{4})-(\d\d)-(\d\d)$/
    or die "Bad date: $date";
say $year + 1;       # 2027

If the match fails, the list is empty, so the assignment is false and die runs. Two details are worth remembering:

  • With no capture groups, a successful match in list context returns the single value 1.
  • With the /g modifier, list context returns every match, as described below.

Named captures

For patterns with several groups, numbers get hard to read. Name a group with (?<name>...) and read it from the special hash %+:

PERL
if ($date =~ /^(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d)$/) {
    say "$+{year}/$+{month}/$+{day}";
}
TEXT
2026/08/03

Named groups also have numbers, so $1 still works alongside $+{year}. The companion hash %- is rarely needed: it holds an array of every group that shares one name. You can refer back to a named group inside the same pattern with \k<name> (see Advanced Regex).

Python spells the syntax (?P<name>...) and returns a dictionary from groupdict(), but the idea is the same. Try both numbered and named access:

Python Numbered and named groups (Python stand-in)

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 input()), it gets the next line from this box.

The modifiers

Modifiers are letters after the closing delimiter that adjust the whole pattern. Several can be combined, as in /gi.

Modifier Effect
/i Case-insensitive matching
/g Global: keep matching after the first match
/m ^ and $ also match at line boundaries inside the string
/s The dot . also matches a newline
/x Ignore whitespace in the pattern and allow # comments
/a \d, \w and \s match ASCII only
/n Plain parentheses do not capture (Perl 5.22 and later)

Anchors and lines. By default ^ matches only at the very start of the string and $ only at the end (or before a final newline). With /m they work per line. If you really mean "start of the whole string" regardless of modifiers, use \A, and use \z for the true end.

PERL
my $text = "alpha\nbeta\ngamma";
my @initials = $text =~ /^(\w)/mg;
say "@initials";                       # a b g
say "yes" if $text =~ /alpha.beta/s;   # the dot crosses the newline

Readable patterns with /x. Long patterns are hard to read on one line. With /x, whitespace is ignored and # starts a comment, so you can lay a pattern out like code. To match a literal space or #, escape it or put it in a character class.

PERL
my $phone = qr/
    ^ \(? (\d{3}) \)?   # area code, optional parentheses
    [\s-]*              # separator
    (\d{3}) - (\d{4}) $ # local number
/x;

Use qr// (a precompiled pattern, see Advanced Regex) when you want to store a pattern in a variable like this.

Python Modifiers (Python stand-in)

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 input()), it gets the next line from this box.

Global matching: /g

Without /g, a pattern matches once. With /g, the behavior depends on context.

List context returns all the matches. With no groups you get the whole matches; with groups you get all the captures, flattened into one list:

PERL
my @numbers = "a1b22c333" =~ /\d+/g;
say "@numbers";                        # 1 22 333

my %config = "host=db1,port=5432" =~ /(\w+)=(\w+)/g;
say "$_ => $config{$_}" for sort keys %config;
TEXT
1 22 333
host => db1
port => 5432

The second example shows why this is popular: a flat list of key, value, key, value assigns straight into a hash.

To count matches, assign to an empty list (). The match runs in list context, and assigning a list to a scalar yields the number of items on the right:

PERL
my $count = () = $text =~ /\bthe\b/gi;

Scalar context matches one piece at a time. Perl remembers where the last match ended (the match position, readable with pos) and the next /g match starts from there. That is what makes a while loop work:

PERL
my $s = "aXbXc";
while ($s =~ /X/g) {
    say "X at index $-[0], ends at ", pos($s);
}
TEXT
X at index 1, ends at 2
X at index 3, ends at 4

The arrays @- and @+ hold the start and end offsets of the last match: $-[0] is where the whole match began, $+[0] is where it ended (the same value as pos). $-[1] and $+[1] give the same for group 1.

If a /g match in a while loop is stopped early with last, the position stays set. A later /g match on the same variable continues from there, not from the start. Reset it with pos($s) = undef; or avoid sharing the variable.

Lexers with \G and /gc

\G anchors a pattern to the current pos. Together with the /c modifier ("do not reset position on failure") it lets you build a small tokenizer that tries one rule after another:

PERL
my $src = "x = 42 + y";
pos($src) = 0;
while (pos($src) < length $src) {
    if    ($src =~ /\G\s+/gc)    { next }
    elsif ($src =~ /\G(\d+)/gc)  { say "NUMBER $1" }
    elsif ($src =~ /\G(\w+)/gc)  { say "WORD $1" }
    elsif ($src =~ /\G(.)/gc)    { say "SYMBOL $1" }
}
TEXT
WORD x
SYMBOL =
NUMBER 42
SYMBOL +
WORD y

Python has no pos variable on strings, but re.finditer gives each match with start() and end(), and pattern.match(text, pos) anchors at a position, like \G:

Python Global matching and positions (Python stand-in)

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 input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. When is it safe to read $1?

    A failed match does not reset the capture variables, so $1 can hold stale text from an earlier success. Use it only after a successful match.
  2. 2. What does my @all = $text =~ /(\d+)/g; return in list context?

    In list context, /g returns all matches (all captures, if the pattern has groups) as one flat list.
  3. 3. Which line reads the group named year?

    Named captures are stored in the special hash %+, so a single value is $+{year}.
  4. 4. What does the /m modifier change?

    /m means multi-line anchors. The dot is /s, case is /i and ignored whitespace is /x.
  5. 5. What does pos($s) report after a scalar-context /g match?

    pos is the offset just after the last match, where the next /g match will start searching.

Summary

  • Parentheses capture text; $1, $2, ... hold it after a successful match and are not cleared by a failed one.
  • A match in list context returns the captures, so my ($a, $b) = $str =~ /(..)(..)/ is the neat way to unpack.
  • Named captures (?<name>...) are read from %+; use (?:...) for grouping without capturing.
  • /i, /m, /s, /x change case handling, anchors, the dot and layout; /g matches repeatedly.
  • /g in list context returns all matches; in scalar context it walks through the string, tracked by pos, @- and @+.
  • \G with /gc builds simple tokenizers.

Next, change text instead of just reading it in Substitution and Transliteration.