in reply to RegEx to remove leading zeros from a hex number

$var =~ s/^0+//g;

It even works on other strings not containing hex numbers!

Replies are listed 'Best First'.
Re^2: RegEx to remove leading zeros from a hex number
by toolic (Bishop) on May 16, 2011 at 20:52 UTC
    ... and gets rid of all zeros. D'oh!
    use warnings; use strict; my $var = '0000'; $var =~ s/^0+//g; print ">>>$var<<<\n"; __END__ >>><<<
      Well, yes. Removing the leading zeros from a string containing only zeros leaves zero zeros. What you do you think should happen? Leaving a string with leading zeros, or a string containing non-zeros? Neither seems to be correct.
        Write a sub. It is "not nearly as 'clever' as one might think" to write a regex one-liner. A clearly commented subroutine, that includes whatever if-statements it may need to clearly and obviously do its job, is much to be preferred.