in reply to substitute leading whitespace
It doesn't work for two reasons.
1) When using /g, Perl won't match from the same point within the string twice, so ^ will only match on the first pass.
2) Your use of ^ ensures that no character other than the first can possibly be replaced.
Use \G instead of ^ to match where the last pass of /g left off. It behaves like ^ on the first pass.
my $string = ' this is a string'; $string =~ s/\G\s/:/g; print("$string\n"); # :::this is a string
Update: Rephrased (2) since it wasn't clear.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: substitute leading whitespace
by tlm (Prior) on Jun 10, 2005 at 10:46 UTC | |
by ikegami (Patriarch) on Jun 10, 2005 at 14:59 UTC | |
by tlm (Prior) on Jun 11, 2005 at 18:55 UTC |