Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Do a named regex group in 5.8?

by Anonymous Monk
on Feb 09, 2016 at 06:09 UTC ( [id://1154707]=note: print w/replies, xml ) Need Help??


in reply to Do a named regex group in 5.8?

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 {%+};'

Replies are listed 'Best First'.
Re^2: Do a named regex group in 5.8?
by AnomalousMonk (Archbishop) on Feb 09, 2016 at 07:37 UTC

    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: note [id://1154707]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 02:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found