Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Problem: $pyrich just saves only one letter not the whole hit.   Why?

Code: my($branch, $pyrich) = /(ct(a|g)a(c|t))(.{20,50})$/i;

$branch is ok
whole hit is ok
But $pyrich prints just one single letter although whole hit is ok so it must be 20-50 letters long.   Where is my fault?

janitored by ybiC: Minor format cleanup, including balanced <code> tags around snippet.

Replies are listed 'Best First'.
Re: Regex: save values
by mrborisguy (Hermit) on Jun 12, 2005 at 18:16 UTC

    It looks to me (and somebody can correct me if I'm wrong), that $branch will contain whatever was found in (ct(a|g)a(c|t)), but then $pyrich will find what was found in (a|g). I think your FOURTH save would be the 20 to 50 character save.

        -Bryan

Re: Regex: save values
by davidrw (Prior) on Jun 12, 2005 at 19:24 UTC
    As already mentioned, it's related to the capture orders. You can use ?: to just do clustering and not capturing to make it easier/clearer (see perldoc perlre):
    if ( /(ct(?:a|g)a(?:c|t))(.{20,50})$/i ){ $branch = $1; # this will be 'ctXaX' $pyrich = $2; # this will be the 20-50 chars at the end }
      Oh dear... you all are so right. It was intended to be
      /(ct(?:a|g)a(?:c|t))(.{20,50})$/i
      of course... you sometimes need the help of others to see clear... LOL
Re: Regex: save values
by dReKurCe (Scribe) on Jun 12, 2005 at 18:44 UTC

    Picking apart your regex by using the following code reveals that the the final block of characters is contained in $4. This is the problem. You were mistakingly assigning $2, witch is matched inside the first group,as this code shows.
    $string="ctaacxxxxxxxxxxxxxxxxxxxxxxx"; $_=$string; $_=/(ct(a|g)a(c|t))(.{20,50})$/i; print "\n$1,$2,$3,$4"; ($branch,$pyrich)=($1,$4); print "\n$branch$pyrich";
    produces:
    ctaac,a,c,xxxxxxxxxxxxxxxxxxxxxxx ctaacxxxxxxxxxxxxxxxxxxxxxxx

    Hope that helps.
      This might also be a good place to use the extended (/x) regex syntax, with whitespace and comments, and strategic indenting, to better understand what's going on.
      use strict; use warnings; "ctaacxxxxxxxxxxxxxxxxxxxxxxx" =~ / ( #begin $1 ct (a|g) # $2 a (c|t) # $3 ) #end $1 (.{20,50})$ # $4 /ix; print "\n$1,$2,$3,$4"; my ($branch,$pyrich)=($1,$4); print "\n$branch$pyrich";
Re: Regex: save values
by Anonymous Monk on Jun 12, 2005 at 18:01 UTC
    I saved it now in an array not in a scalar and joined the fields... Now everything works fine.