Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
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 rifling through the Monastery: (7)
As of 2015-07-07 01:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls