Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx Blues 1

Status
Not open for further replies.

Zoom1234

Programmer
Oct 30, 2003
116
BE
Hi,

Why
Code:
if ( $str =~ /(.*)TEST/ ) {
...
}
if ( $str =~ /(.*)TEST1/ ) {
...
}
$cwd = $1 ;
this doesn't work.
But this works fine
Code:
if ( $str =~ /(.*)TEST/ ) {
...
$cwd = $1 ;
}
if ( $str =~ /(.*)TEST1/ ) {
...
$cwd = $1 ;
}
 
When you try to take a numbered variable from a regexp in the `if' statement like that, it only remains in scope inside the `if' statement's block. In other words, the variable can no longer be accessed once you hit the closing parenthesis.
 
without knowing what you are doing, you can write that in one regexp:

Code:
if ($test =~ /(.*)TEST1?/) {
   $cwd = $1;
}

or group the regexp's use the 'or' operator:

Code:
if ($test =~ /(.*)TEST/ or $test =~ /(.*)TEST1/) {
   $cwd = $1;
}

ishnid aanswered your question though.
 
Yes, ishnid reply is satisfactory. Have a star. Thanks.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top