Sunday, August 2, 2009

Perl Regex: remove leading zeroes in a string?

I want to rename some files using the rename utility.





I've tried 's/(\d*0)(\d*)/#$2/i' which does the following:


file name 01.ext -%26gt; file name #1.ext





but, it doesn't work when there is no leading zero:


file name 10.ext -%26gt; file name #.ext





Any suggestions?

Perl Regex: remove leading zeroes in a string?
Use the following regex instead:





s/(0*(\d+))/#$2/i





It will swallow entire number and will strip out leading zeroes, if any.
Reply:You need to use the special regex character \A which means "the beginning of the string". So try this:





$sFname =~ s/\A0+//gx;

flower arranging

No comments:

Post a Comment