Home: Perl Programming Help: Regular Expressions:
capturing parentheses in subseraches



PGScooter
stranger

Jun 12, 2008, 1:21 AM


Views: 7995
capturing parentheses in subseraches

Hi, I know I've read this a couple of times, but forgot how it is correctly done. Suppose I do a regex search, condition on it and within that if block, I do another regex search. How do I refer to the capturing parentheses in the subsearch? Because don't $1,$2,... refer to the main search, not the nested one?

if ($string=~/(\w*)end/) {

if ($diffstring=~/$1five(\w*\d{2})/) {

print "don't know"; #how do I get what's in (\w*\d{2})?

}

}

thanks
The more you teach me, the more I learn. The more I learn, the more I teach.


KevinR
Veteran


Jun 12, 2008, 8:35 AM


Views: 7982
Re: [PGScooter] capturing parentheses in subseraches






Code
   

if ($string=~/(\w*)end/) {

$foo = $1;

if ($diffstring=~/${foo}five(\w*\d{2})/) {

print $1;

}

}



-------------------------------------------------


PGScooter
stranger

Jun 12, 2008, 1:30 PM


Views: 7967
Re: [KevinR] capturing parentheses in subseraches

ah, that definitely makes sense! For some reason I thought I had seen a special way to refer to a nested one, but your way seems perfect.



Thanks!
The more you teach me, the more I learn. The more I learn, the more I teach.