Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

A list of backreferences from a regex

by ISAI student (Scribe)
on Mar 27, 2013 at 08:35 UTC ( #1025645=perlquestion: print w/ replies, xml ) Need Help??
ISAI student has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. In Ruby & Python, one can have a regular expression, with backreferences, and then access the backrefernces as a list, and it's sub lists. For example, The Ruby code below returns the 1st backreference from a regexp

return line.match(/^integer.*?\"(\S+)\"/)[1]

I am looking for something other than:  @a =($1,$2,$3,$4) ;
And rather:

@a=$foo[1..4];
I could not find something similar to that in PERL, other than a reference to $+ $-, which is not what I wanted and has a warning about performance. Any ideas?

Comment on A list of backreferences from a regex
Select or Download Code
Re: A list of backreferences from a regex
by Corion (Pope) on Mar 27, 2013 at 08:42 UTC

    If you want variable access to the matches, you can only do that through @- and @+, as perlvar documents.

Re: A list of backreferences from a regex
by hdb (Prior) on Mar 27, 2013 at 08:48 UTC

    Something like

    my $string = "put your own text here"; my @a = $string =~ /(\w+) (\w+) (\w+) (\w+) (\w+)/; print join ":", @a;

    ?

Re: A list of backreferences from a regex
by dave_the_m (Parson) on Mar 27, 2013 at 10:12 UTC
    my $string = "a b c"; my $middle = ($string =~ /(\w) (\w) (\w)/)[1]; print "middle=[$middle]\n"; # prints b

    Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-10-25 01:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (139 votes), past polls