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


In reply to Re: (Ovid) Re: Regex to zero suppress by mkmcconn
in thread Regex to zero suppress by Theodorus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.