in reply to Regular Expression

As discussed in Extracting matches, when you use a regular expression in list context, it returns the captured values. Thus:

my ($Rkey_id, $Rkey_val) = $line =~ /Id="(.*?)".*?>(.*?)</

Update: extraneous parenthesis in regex fixed - nice catch, AnomalousMonk

Replies are listed 'Best First'.
Re^2: Regular Expression
by Sun751 (Beadle) on Jun 24, 2009 at 04:55 UTC
    my ($Rkey_id, $Rkey_val) = $line =~ /Id="(.*?)".*?>(.*?)</) above code return correct result but when i try this,
    my $line = "AAA:BBB:CCC"; my $var = $line =~ /.*?:(\w+):.*?$/; print "$var\n";
    If i am just interested in "BBB", above code returning 1 rather than BBB, Could any one guide me why? and how can I achieve this? Please. Cheers
      It is all in the context.

      Your assignment to "my $var" is in a scalar context.

      The result of the regular expression is a single item in a list context.

      You can get the desired result by:

      my ($var) = $line =~ /.*?:(\w+):.*?$/; # Declare $var in a List contex +t.

           Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)