anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: regex substitute(resolved)
by Athanasius (Archbishop) on May 16, 2013 at 04:13 UTC | |
by anaconda_wly (Scribe) on May 16, 2013 at 04:26 UTC | |
Re: regex substitute(resolved)
by hdb (Monsignor) on May 16, 2013 at 06:29 UTC |
Back to
Seekers of Perl Wisdom