Lets learn about a simple regular expression, that will remove all the leading zeros from a given string. This regexp will work for Python, Perl, PHP, and ruby so far I know. But still you can try the regexp with other languages for sure.
The regular expression is:
The above expression will remove all leading zeros. But remember, if the string contains all zeros, for example "0000000" it will remove all zeros, and keep an empty string as returns.
If you want to keep see it 0 after removing all leading zeros from "000000", the Perl regular expression will be as below with an example.
my $string = "0000000";
$string =~ s/^0+(.)/"$1"/e;
You know PHP is a bit different to perform regex matching, and replacing. I'm using the same above expression, using preg_replace() of php. Here goes the example that will remove all the leading zeros, but keep single zero, if all are zero.
$string = "0000000";
$string = preg_replace('/^0+(.)/', "$1", $string);
So far I don't know how to use the /e modifier of Perl into Ruby or Python. That's why no example of Ruby and Python for second portion. You have to be happy with replacing all zeros with empty :-p, If anyone know this just post here as comment. Will be great if you do. But for sure, I'll find myself soon about how to do it.