Monday, March 31, 2008

IPv4/v6 Regular Expressions

I found these useful regexes in my notes. I probably found this somewhere on the Internet, but I forgot where.

IPv4 address

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

IPv6 address

^[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}$

Note that this does not work with shortened form such as dead::beef. dead:0000:0000:0000:0000:0000:0000:beef must be used instead.

No comments: