I got a string $ou "# PATH=$PATH:$NAS_DB/sbin:$NAS_DB/bin:$NAS_DB/sbin" and want to delete the heading # and the white space around.
my $ou ="# PATH=\$PATH:\$NAS_DB/sbin:\$NAS_DB/bin:\$NAS_DB/sbin"; print __FILE__." line".__LINE__." trace: ".$ou."\n"; if($ou =~ /^\s*#\s*(PATH=.*\$NAS_DB\/bin.*)$/) { my $str = $1; $ou=~s/^\s*#\s*PATH=.*\$NAS_DB\/bin.*$/$str/; print __FILE__." line".__LINE__." trace: ".$ou."\n"; }
When I remove $str, replace it with $1, the output is empty. Not what I want. So why? Every thanks.
OK, I got it. Seems $1 was reset after every matching.my $ou ="# PATH=\$PATH:\$NAS_DB/sbin:\$NAS_DB/bin:\$NAS_DB/sbin"; print __FILE__." line".__LINE__." trace: ".$ou."\n"; if($ou =~ /^\s*#\s*(PATH=.*\$NAS_DB\/bin.*)$/) { $ou=~s/^\s*#\s*PATH=.*\$NAS_DB\/bin.*$/$1/; print __FILE__." line".__LINE__." trace: ".$ou."\n"; }
In reply to regex substitute(resolved) by anaconda_wly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |