sathiya.sw has asked for the wisdom of the Perl Monks concerning the following question:

can some body explain me, how does the following assigns the data to the variable correctly.
$_ = 'ab$=$12a3$=$45$=$4thfield$=$5th'; ( $a, undef , $b ) = $_ =~ /(\w+)\$=\$(\w+)\$=\$(\w+)/; print "=>$a<==>$b<=\n";
My question is:
1. How does it only stores the captured things to the variable, why can't it store the $=$ into the variable ?
2. Is it internally mentioned that it should store the $1, $2, $3 ?? or any other mechanism ?
Sathiyamoorthy

Replies are listed 'Best First'.
Re: regex; how it works
by ikegami (Patriarch) on Feb 28, 2009 at 09:11 UTC

    The match operator in list context returns the captures. This is actually documented in perlop. (Not in perlre, which documents regexp patterns.)

    why can't it store the $=$ into the variable ?

    What makes you think it can't? If you're not capturing the right thing, move the parens to capture the right thing.

      This clarifies me "The match operator in list context returns the captures." .. Thanks.
      Sathiyamoorthy
Re: regex; how it works
by codeacrobat (Chaplain) on Feb 28, 2009 at 17:49 UTC
    See YAPE::Regex::Explain
    The regular expression: (?-imsx:(\w+)\$=\$(\w+)\$=\$(\w+)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- \w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \$ '$' ---------------------------------------------------------------------- = '=' ---------------------------------------------------------------------- \$ '$' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- \w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- \$ '$' ---------------------------------------------------------------------- = '=' ---------------------------------------------------------------------- \$ '$' ---------------------------------------------------------------------- ( group and capture to \3: ---------------------------------------------------------------------- \w+ word characters (a-z, A-Z, 0-9, _) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
Re: regex; how it works
by locked_user sundialsvc4 (Abbot) on Feb 28, 2009 at 16:42 UTC

    The matched regular-expression, which contains parenthesized groups to “capture” various matching-parts out of the input string, returns those matches as a list. The construct ( $a, undef , $b ) = ... is a list assignment, albeit a faintly-unusual one due to the presence of undef as the second term. The corresponding elements of the returned list are assigned as-shown.

    BTW, this is why you can swap two variables so easily:   ($a,$b) = ($b,$a); ... presto!

Re: regex; how it works
by Corion (Patriarch) on Feb 28, 2009 at 08:59 UTC