Solved

Regex to reformat IP address

Posted on 2014-03-16
6
348 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex HELP 2 35
Perl script to process a .csv file 18 44
Error in Angular 2 when showing in grid 4 19
what is the best Integrated development environment 2 18
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

791 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