You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Yuren Li <yu...@gmail.com> on 2007/05/04 09:51:49 UTC

About the regular expression problem

Hi,
I have a problem when I use regular expression.
The sample code:
[- $_ = "One Two Three" -]
[$ if(/(\S+) (\S+) (\S+)/) $]
The words are: [+$1+], [+$2+], [+$3+]
[$ endif $]

The result should be "The words are: One, Two, Three," But I got the
result "The words are: One, Two,."
After trying, I found that I could just get the match variables $1 and
$2, and the match veriables $3, $4 etc, could not be used in embperl.

There is no problem about this in pure perl code such as
$_ = "One Two Three";
if(/(\S+) (\S+) (\S+)/) {
    printf "The words are: $1, $2, $3";
}

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: About the regular expression problem

Posted by Gerald Richter - ECOS GmbH <ri...@ecos.de>.
HI,

> I have a problem when I use regular expression.
> The sample code:
> [- $_ = "One Two Three" -]
> [$ if(/(\S+) (\S+) (\S+)/) $]
> The words are: [+$1+], [+$2+], [+$3+]
> [$ endif $]
> 
> The result should be "The words are: One, Two, Three," But I 
> got the result "The words are: One, Two,."
> After trying, I found that I could just get the match 
> variables $1 and $2, and the match veriables $3, $4 etc, 
> could not be used in embperl.
> 
> There is no problem about this in pure perl code such as $_ = 
> "One Two Three";
> if(/(\S+) (\S+) (\S+)/) {
>     printf "The words are: $1, $2, $3";
> }
> 


Embperl does not do anything with $3, $4 etc. I guess this is a scoping 
problem. I would write


 [$ if(($a, $b, $c) = (/(\S+) (\S+) (\S+)/)) $]
 The words are: [+$a+], [+$b+], [+$c+]
 [$ endif $]

That makes sure $1, $2 etc. does not go out of scope

Gerald


** Virus checked by BB-5000 Mailfilter **

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org