?
Solved

Regex to reformat IP address

Posted on 2014-03-16
6
Medium Priority
?
361 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 2000 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 2000 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

765 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