Next | Regular Expression Mastery | 56 |
Consider:
# $file is "report.pl" or "/usr/local/bin/report.pl" ($path, $name, $suff) = $file =~ m{ (.*/)?(.*)\.(.*)};
When $file is /usr/local/bin/report.pl
/usr/local/bin/ report . pl -----path------ -name- suff $1 $2 $3
But what about when $file is report.pl and has no path?
Since the (.*/)? is 'skipped', will (.*)\.(.*) be $1 and $2?
No. The parentheses are numbered at compile time
The value of $file cannot affect that
report . pl path -name- suff $1 $2 $3
$path here is undefined
Similarly /(a+)|(b+)/
If there are any a's, they will be in $1
If there are b's but no a's, the b's will be in $2, and $1 will be undefined
$1 always contains the a's; $2 always contains the b's
Next | Copyright © 2002 M. J. Dominus |