Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

can i get a matched values as an array

by balakrishnan (Monk)
on Feb 17, 2009 at 08:12 UTC ( #744313=perlquestion: print w/ replies, xml ) Need Help??
balakrishnan has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I have a line of code to match a line with some regular expression.
$str =~ s/age ([0-9]+) name ([a-z]+)/$&/; <br>
By using the above,I can get the matched values by printing $1 and $2 variables. Will i get matched result as an array?

Note : Am not interested to explicitly copy the matched variables into an array.
Thanks.

Comment on can i get a matched values as an array
Download Code
Re: can i get a matched values as an array
by irah (Pilgrim) on Feb 17, 2009 at 08:39 UTC
    Here I give the example,
    @words = ($str =~ /(\w+)/g);
    The array have a words from index 0. If it is three words, each word will be in each index from 0 to 2. I got this information from perlrequick. Further more information look into the man page.
Re: can i get a matched values as an array
by AnomalousMonk (Monsignor) on Feb 17, 2009 at 08:41 UTC
    This can be done by capturing a match to an array:
    >perl -wMstrict -le "my $s = 'age 23 name john'; my @an = $s =~ m{ age \s+ (\d+) \s+ name \s+ ([a-z]+) }xms; print qq{@an}; " 23 john
    Note that if there is an overall match, each capturing group of the regex will return something to the array even if that particular group was not a part of the overall match:
    >perl -wMstrict -le "my $s = 'age 23 name john'; my @an = $s =~ m{ name \s+ ([a-z]+) | address \s+ (\d+) }xms; my $captured = join ':', @an; print qq{:$captured:}; " Use of uninitialized value in join or string at -e line 1. :john::
    The values from unmatched capture groups are undefined and can be detected and filtered:
    >perl -wMstrict -le "my $s = 'age 23 name john'; my @an = $s =~ m{ name \s+ ([a-z]+) | address \s+ (\d+) }xms; defined or $_ = 'UNdefined' for @an; my $captured = join ':', @an; print qq{:$captured:}; " :john:UNdefined:
    See perlop, the section on Regexp Quote Like Operators, subsection on the m/PATTERN/msixpogc operator, about the seventh paragraph in.

    Note: in Perl 5.10, this can also be done with named capture buffers.
    Look for (?<NAME>pattern) in perlre Extended Patterns.

    Updates:

    1. Added reference links.
    2. Added note re: named capture buffers.
Re: can i get a matched values as an array
by Utilitarian (Vicar) on Feb 17, 2009 at 08:49 UTC
    Hi balakrishnan, To the best of my knowledge you can't use a substitution. however you could use split to achieve this.
    #!/usr/bin/perl use strict; my $i; my @values; my $string ="age 42 name Utilitarian FamilyName"; ($i,@values)= split(/age|name/, $string); for ($i=0;$i<@values;$i++){ print "$i, $values[$i]\n"; }
    This is ugly, but does what you want in one step (ie.applies the regex and makes the matches available in an array.
Re: can i get a matched values as an array
by oshalla (Deacon) on Feb 17, 2009 at 14:50 UTC

    BTW, your:

    $str =~ s/age ([0-9]+) name ([a-z]+)/$&/ ;
    is matching something and then replacing it by itself. Not only does this seem a little pointless (why not just  m/age ([0-9]+) name ([a-z]+)/ ?), but the documentation warns that:
    The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches.
    so using $& when you don't absolutely need it looks like a mistake...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-08-20 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (110 votes), past polls