[Gllug] Re: Bash Data validation libraries
Russell Howe
rhowe at siksai.co.uk
Wed Feb 15 01:05:46 UTC 2006
On Tue, Feb 14, 2006 at 11:27:43PM +0000, Nix wrote:
> Verifying valid IPs is a bit too hard to sanely do in a regexp (although
> it is *possible*).
Not hard, just ugly :)
^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
Seems to be about the best way I can think of.
$octetpattern followed by ., 3 times, followed by $octetpattern
where $octetpattern is either
25[0-5] # matches any integer in the range 250-255 inclusive
2[0-4][0-9] # matches any integer in the range 200-249 inclusive
1[0-9]{2} # matches any 3 digit integer where the first digit is 1, i.e. 100-199
[1-9]?[0-9] # the rest (0-99)
That's the least obfuscated way I can think of doing it concisely (you
could always do ^((1|2|3|...|255)\.){3}(1|2|3|...|255)$, but that just
seems wasteful :)
And of course the above doesn't allow for leading zeros, but the
modifications to do so are trivial
--
Russell Howe | Why be just another cog in the machine,
rhowe at siksai.co.uk | when you can be the spanner in the works?
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list