Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Regex to zero suppress

by Theodorus (Initiate)
on Dec 29, 2000 at 20:05 UTC ( [id://48823]=perlquestion: print w/replies, xml ) Need Help??

Theodorus has asked for the wisdom of the Perl Monks concerning the following question:

I would like a regex to replace leading zeroes in a number. For example, a ten-digit number string has 5 leading (leftmost) zeroes. I would like to replace each leading zero with a space, i.e. 5 leading zeroes with 5 leading spaces. I tried s/^0+/ /g but it replaced all leading zeroes with only one space character. How may I accomplish this?

Replies are listed 'Best First'.
(Ovid) Re: Regex to zero suppress
by Ovid (Cardinal) on Dec 29, 2000 at 20:13 UTC
    See perlre. It notes that
    \G Match only where previous m//g left off (works only with /g)
    Try the following:
    perl -e "$num = \"0000012345\"; $num =~ s/\G0/ /g;print $num"

    Cheers,
    Ovid

    Update: Just saw merlyn's response and wanted to note that I have Effective Perl Programming. Where the heck do you think I got that regex? :)

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Just tried that with "0.5"" and didn't like the result. For a fix make the substitution the not quite so nice:
      s/\G0(?=\d)/ /g;
      Update
      Fixed the RE for the common case of '0' as well...
      $num =~ s/\G0/ /g;
      Ahh yes. I remember creating that in response to a similar question a few years ago in comp.lang.perl.misc, and then Joseph copied it for his Effective Perl Programming book, and now it's become part of the culture. Not quite as impactful as the Schwartzian Transform, but I remember getting a few "ooohs" and "aaaahs" over the simplicity of that.

      -- Randal L. Schwartz, Perl hacker

      Please take the following with a grain of salt. Ovid has already given the best answer.
      It makes me wince, now, but it was actually used in production to strip leading zeros from numbers in a list, in the first real Perl program I ever wrote.

      #!/usr/bin/perl -w @numberarray = qw(00000900011 10001000100 00022200101); for $digits (@numberarray){ @temparray = split (//,$digits); $incr=0; while ($temparray[0] eq 0) { shift @temparray; $incr++; } unshift (@temparray," "x$incr); $digits = join('',@temparray); print "$digits\n"; }

      I couldn't discover a better way than to shift out the zeroes and unshift the replacing text, then join the result together.
      It isn't pretty, but, as far as I can tell, it doesn't break.

      mkmcconn
      novice


      Update:
      And now, a monk, I never would have posted this (wonderful what a few weeks at perlmonks can do), but if I had, I would have suggested to novice mkmcconn that even taken with a grain of salt, numerous difficulties in his solution could be avoided by writing it this way:

      #!/usr/bin/perl -wl use strict; my @numberarray = qw(000000 000002 001110 010201 010010 001000 0001 +20 010030); for (@numberarray){ next if $_ == 0; my @array = split //; my $incr = 0; while ($array[0] eq 0) { shift @array; $incr++; } unshift (@array,"\x20" x $incr); print @array; }
      And then would have advised him (as a friend) of the topical difference between "Regex to zero suppress" and "Subroutine to remove zero-padding".

      It will be interesting to find out what advice I might give myself someday, about this update.
      mkmcconn

Re: Regex to zero suppress
by davorg (Chancellor) on Dec 29, 2000 at 20:19 UTC

    Something like this perhaps:

    my @nums = qw(000001 000010 000100 001000 010000 100000); foreach (@nums) { s/^(0+)/' ' x length $1/e; print "|$_|\n"; }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Regex to zero suppress
by Hot Pastrami (Monk) on Dec 29, 2000 at 20:49 UTC
    Don't forget you can also do:
    $numString =~ tr/0/ /;

    Update: Um, don;t use this. It won't work right. I'm dumb.

    Hot Pastrami
        Oh yes, good point old chap. I should learn not to post anything if the hour has "AM" in it.

        Hot Pastrami

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://48823]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found