ip address
Feb. 10th, 2011 10:50 amI 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
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.