in reply to substitute leading whitespace

I believe I saw this first in "Effective Perl Programming":
$str =~ s/\G\s/:/g;
The \G anchor means "anchor to where the last //g match left off". When no global match has been done yet, \G is synonymous to ^. Thus, each leading space is turned into a colon until a space is not found following the last one.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: substitute leading whitespace
by Anonymous Monk on Jun 10, 2005 at 06:30 UTC
    luuuuurvely. Works like a charm. Thankies.