Solved

Regex to reformat IP address

Posted on 2014-03-16
6
345 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# GridRow get Old/New Value 1 62
Automating receiving uploaded files via HTTPS instead of SFTP 3 41
Regular Expression Calculator Tester 2 71
tomcat startup error 5 59
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

816 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now