Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Do a named regex group in 5.8?

by crusty_collins (Friar)
on Feb 08, 2016 at 20:57 UTC ( [id://1154673]=perlquestion: print w/replies, xml ) Need Help??

crusty_collins has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if I can do a named group regex with some kind of trickery like is available in 5.10?
any ideas?


snippet from http://www.regular-expressions.info/refext.html
(?<x>abc){3} matches abcabcabc. The group x matches abc.

"We can't all be happy, we can't all be rich, we can't all be lucky – and it would be so much less fun if we were. There must be the dark background to show up the bright colours." Jean Rhys (1890-1979)

Replies are listed 'Best First'.
Re: Do a named regex group in 5.8?
by AnomalousMonk (Archbishop) on Feb 08, 2016 at 21:56 UTC

    Another pre-5.10 approach is returning the list of capture group contents from the regex in list context and assigning directly to named variables:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xxx foo , 987;-+--++-+%yyy'; ;; my $sep = qr{ \s* [,;] \s* }xms; ;; my ($letters, $digits, $signs) = $s =~ m{ ([a-z]+) $sep (\d+) $sep ([-+]+) }xms; print qq{letters '$letters' digits '$digits' signs '$signs'}; " letters 'foo' digits '987' signs '-+--++-+'
    Of course, this doesn't give you the  \g{X} etc. backreference capabilities, but that's life.


    Give a man a fish:  <%-{-{-{-<

Re: Do a named regex group in 5.8?
by Cristoforo (Curate) on Feb 08, 2016 at 21:16 UTC
      This module comes with perl and it is used to implement %+ and %-, not the named captures itself. It has no use with 5.8.
        zwon
        I didn't know that this module would not work with version 5.8 - thanks for the correction.
Re: Do a named regex group in 5.8?
by Anonymous Monk on Feb 09, 2016 at 06:09 UTC

    The (?{}) and (??{}) constructs are available for use in perl5.8, but you'd have to ask yourself if such ugliness is really worth it:

    $ perl -MData::Dumper -e 'q(abc de f abcdef) =~ m/(?<x>\w{3}).*\k<x>/ +and print Dumper {%+};' $ perl5.8.8 -MData::Dumper -e 'q(abc de f abcdef) =~ m/(?{$+{x} = pos} +)(\w{3})(?{$+{x} = substr($_, $+{x}, -$+{x}+pos)}).*(??{$+{x}})/ and +print Dumper {%+};'

      This can be made slightly less ugly by

      c:\@Work\Perl>perl -wMstrict -le "use Data::Dumper qw(Dumper); ;; print qq{perl version $]}; ;; q(abc de f abcdef) =~ m/ ( (\w{3}) (?{ $+{x} = $^N; }) .* (??{ $+{x}; }) ) /x and print qq{\$1 '$1' \n}, Dumper \%+; " perl version 5.008009 $1 'abc de f abc' $VAR1 = { 'x' => 'abc' };
      if it's really necessary to have named backreferences in the regex via a hash.

      Otherwise, I'm not sure I see its advantage over something like

      c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; q(abc de f abcdef) =~ m/ ( (\w{3}) .* \2 ) /x and print qq{\$1 '$1'}; " perl version 5.008009 $1 'abc de f abc'
      or perhaps like
      c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my $match = my ($whole, $first_part) = q(abc de f abcdef) =~ m/ ( (\w{3}) .* \2 ) /x ;; print qq{first part '$first_part' whole '$whole'} if $match; " perl version 5.008009 first part 'abc' whole 'abc de f abc'


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1154673]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found