27

Is it possible to perform a named-group match in Perl's regex syntax as with Python's? I always bind the $n values to proper names after matching, so I'd find it more convenient to do it in the regex itself if it's possible.

Python does it like so:

>>> import re
>>> regex = re.compile(r'(?P<count>\d+)')
>>> match = regex.match('42')
>>> print match.groupdict()
{'count': '42'}

I know the ?P indicates that it's a Python-specific regex feature, but I'm hoping it's in Perl in a different way or was added later on. Is there any way to get a result hash in a similar manner in Perl?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
cdleary
  • 69,512
  • 53
  • 163
  • 191
  • 1
    For search engine findability: named groups are sometimes also referred to as "symbolic group names". – conny Jul 11 '09 at 20:24

5 Answers5

51

Perl uses (?<NAME>pattern) to specify names captures. You have to use the %+ hash to retrieve them.

$variable =~ /(?<count>\d+)/;
print "Count is $+{count}";

This is only supported on Perl 5.10 and higher though.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
21

As of Perl 5.10, Perl regexes support some Python features, making them Python compatible regexes, I guess. The Python versions have the "P" in them, but all of these work in Perl 5.10. See the perlre documentation for the details:

Define a named capture buffer. Equivalent to (?<NAME>pattern).

(?P<NAME>pattern)

Backreference to a named capture buffer. Equivalent to \g{NAME}.

(?P=NAME)

Subroutine call to a named capture buffer. Equivalent to (?&NAME).

(?P>NAME)

Although I didn't add the Python-compatibility to the latest edition of Learning Perl, we do cover the new Perl 5.10 features, including named captures.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
13

As couple of people said perl 5.10 has named groups.

But in previous perls you can do something, not as convenient, but relatively nice:

my %hash;
@hash{"count", "something_else"} = $string =~ /(\d+)\s*,\s*(\S+)/;

and then you can use:

$hash{"count"} and $hash{"something_else"}.

5

AFIK PCRE has named group capturing as:

(?'NAME'pattern)
(?<NAME>pattern)

You can find info here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
0

I use the %{^CAPTURE} hash (for readability).

It is English version for %+ as mentioned above by Leon Timmermans.

For example, a code I wrote for capturing PHP version:

#! /usr/bin/env perl

use v5.32;
use warnings;
use English;

my $output = `php -v`;

$output =~ m(PHP (?<version>\d.\d.\d\d)); # named capture group

say ${^CAPTURE}{version}; # instead of $1

Within the regex pattern you can reference the named capture groups as \g{NAME}.

TIMTOWTDI with Perl, so use the one which suits you the best.

Elvin
  • 166
  • 7