For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > How to remove trailing zero









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to remove trailing zero
Khairul Azmi

2004-10-25, 3:56 am

Can somebody help me how to remove the trailing zero from an ip address

$ip_add = "010.200.020.000";

The output should be "10.200.20.0";
I''ve been trying many regex techniques but still unsuccessful.
Thanks

Azmi
Chris Devers

2004-10-25, 3:56 am

On Mon, 25 Oct 2004, Khairul Azmi wrote:

> Can somebody help me how to remove the trailing zero from an ip address
>
> $ip_add = "010.200.020.000";
>
> The output should be "10.200.20.0";
> I''ve been trying many regex techniques but still unsuccessful.


It's probably possible to do this other ways, but oh well:

$ perl -le '$ip_add = "010.200.020.000"; $ip_add =~ s/0*([0-9]+)/$1/g; print $ip_add'
10.200.20.0
$

So the statement to use (as I'm approaching it) is:

$ip_add =~ s/0*([0-9]+)/$1/g;


--
Chris Devers
John W. Krahn

2004-10-25, 3:56 pm

Khairul Azmi wrote:
> Can somebody help me how to remove the trailing zero from an ip address
>
> $ip_add = "010.200.020.000";
>
> The output should be "10.200.20.0";
> I''ve been trying many regex techniques but still unsuccessful.


$ perl -le'$_ = "010.200.020.000"; print; s/\b0+(?=\d)//g; print'
010.200.020.000
10.200.20.0


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com