Solved

Regex to reformat IP address

Posted on 2014-03-16
6
352 Views
Last Modified: 2014-03-18
Hi Experts,

I'm interested to see if there's a simple regex (or other concise option) to convert addresses like this:
    123.45.6.255
to:
    123045006255
i.e. 3 digits for each octet, padding with leading zeros, to they are all 12 digits long with no periods.

Here's the Perl code I wrote:
    ($ip1, $ip2, $ip3, $ip4) = split('\.', $ENV{REMOTE_ADDR});
    $ip_std = sprintf("%03d%03d%03d%03d", $ip1, $ip2, $ip3, $ip4);
And that does the job, but I'm wondering if it can be simplified.

I guess a few regex's could:
    a) Prefix all octets with '00', then
    b) Keep the last 3 digits only, then
    c) Remove the periods.
Sometime like this:
    ($ip = $ENV{REMOTE_ADDR}) =~ s/(\d+?)/00$1/g;
    $ip =~ s/(\d{1,2})(\d\d\d)/$2/g;
    $ip =~ s/\.//g';
But I'm wondering if there's something more elegant.


Also, I'm wondering if I should I be making my website code so it can handle visitors with IPv6 addresses.  When could they start visiting websites?

Thanks.
tel2
0
Comment
Question by:tel2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39933526
($ip = $ENV{REMOTE_ADDR}) =~ s/(\d+)\.?/sprintf"%03d",$1/eg;    
or
$ip=sprintf"%03d%03d%03d%03d", split'\.', $ENV{REMOTE_ADDR};
or
$ip=sprintf"%03d"x4, $ENV{REMOTE_ADDR}=~/\d+/g;
0
 
LVL 12

Author Comment

by:tel2
ID: 39933565
Beautiful work, ozo.

We'll see if anyone can offer alternatives before I close this.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 39933576
If you want to avoid sprintf
s/(\d+)/00$1/g,s/0*(\d{3})\.?/$1/g for $ip=$ENV{REMOTE_ADDR};
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Closing Comment

by:tel2
ID: 39938218
Good answer again, ozo, and it seems you have no competition, so I will now close this.

That last one was an interesting use of a 'for' "loop" (without looping).  Nice work.  Did you use the 'for' purely to make the code a bit (or even a few bytes) more concise, ozo?  I guess this is the long-hand alternative:
    ($ip = $ENV{REMOTE_ADDR}) =~ s/(\d+)/00$1/g;
    $ip =~ s/0*(\d{3})\.?/$1/g;

Thanks for your efforts (x4).

TRS
0
 
LVL 84

Expert Comment

by:ozo
ID: 39938482
Illustrating some variations.  I didn't think you were asking for just one way to do one thing, since you already had that.
You could mix and match among the techniques to come up with a lot more combinations, but I just wanted to give a few examples of some of the techniques so you could decide for yourself which combination you like best.
Other functions that might be used include eval and (un)pack, but in this application it didn't seem to end up simplified.
0
 
LVL 12

Author Comment

by:tel2
ID: 39938488
Thanks ozo.

Yes, it's good to see all those different options.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to dynamically set the form action using jQuery.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question