Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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:

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?

Replies are listed 'Best First'.
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 dave_the_m (Prior) 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


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;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025645]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-03-25 13:49 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (311 votes). Check out past polls.