tags:

views:

1166

answers:

5

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?

A: 

Not as far as I know... but what's wrong with the good old way:

"42" =~ /(\d+)/;
my $count = $1;

Cameron
perl 5.10 supports named capture groups
aku
The old way has several problems for usual regexes with several captures, including sequencing and knowing what should show up where.
brian d foy
lexu
@lexu There is something wrong with the old way, examine this example http://gist.github.com/142109 It takes five extra lines to untangle the captures into the right place and it was very a simple case. Here is a slightly more complicated real world case: http://gist.github.com/142116 And the full regex only gets more complicated.
Chas. Owens
+4  A: 

AFIK PCRE has named group capturing as:

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

You can find info here.

CMS
+16  A: 

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.

Leon Timmermans
`/(?'count'\d+)/` works as well.
Brad Gilbert
You can also use `%-` to access the captures.
Brad Gilbert
+12  A: 

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
Please clarify which is the Perl syntax and which the Python.
ysth
The syntax with the extra P is python.
Leon Timmermans
They are all Perl syntax.
brian d foy
I have tried them all to see which work in Perl.Yes, all of them can be used in latest Perl.
Aftershock
+5  A: 

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"}.

depesz
+1 I'd prefer this over $1, $2, ... anyday! Thanks for the tip.
cdleary