Feb. 10th, 2011

ip address

Feb. 10th, 2011 10:50 am
ljplicease: (Open Trash)

I frequently get the question how to match an IP address because regexen are associated strongly with Perl where they are intrinsic to the language and not an add-on or standard library. I usually write this:

  • /(\d{1,3}\.){3}\d{1,3}/

although it isn't strictly correct since it matches 301 and other illegal octets. Better would be something like this:

  • /\b(?:(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\b/

Stop me if you see an error in it, but I believe that to be correct. I'm using ?: because maybe I don't want to capture the individual octets. I'm using \b because maybe I am matching IP addresses embedded inside a string (if I wanted to strictly check if a string is an IP address, I would use ^ and $ instead). As it is I probably would want to capture the IP address itself, so the whole thing should be in ( and ). None of this is ever clear from the question, although it would be obvious if it were an actual problem in a program that I was writing.

Also, although this regex is arguably better than the previous one, it doesn't allow leading zeros, which arguably should indicate an octet in octal, or 0x to indicate a hex number, although many programs will accept these conventions. Also IPv6 use a completely different display (and storage) format for IP addresses, but they are strictly speaking IP addresses so I really should match them as well.

My point is I feel it is a badly ambiguous question, but if you are a Perl programmer you will be expected at some point in your career to answer it “correctly”.

update: I cannot leave well enough alone, here is a regex that matches an IPv4 address in either decimal, octal or hex, capturing the whole IP address into $1:

  • /\b((?:(?:0[0-7]{1,3}|0x[0-9a-f]{1,2}|[1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:0[0-7]{1,3}|0x[0-9a-f]{1,2}|[1-9]?\d|1\d\d|2[0-4]\d|25[0-5]))\b/i

Octal and hex are less complicated than decimal, we really should have either 8 or 16 fingers.

Profile

ljplicease: (Default)
ljplicease

April 2017

S M T W T F S
      1
23 45678
9101112131415
16171819202122
23242526272829
30      

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 11th, 2025 05:37 pm
Powered by Dreamwidth Studios