Page 1 of 1

regex for an ip

Posted: Sat Dec 10, 2005 10:04 am
by Luke
Can anybody provide me with a regex that checks IP validity (I thought "[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}) would work but it doesn't. I'm new at regex

Posted: Sat Dec 10, 2005 10:12 am
by Jenk
This should be in the RegEx forum, as it is about... RegEx.

Code: Select all

preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]{1,5})?$/", $ipaddr);

Posted: Sat Dec 10, 2005 12:03 pm
by John Cartwright
Moved to Regex.

Posted: Sat Dec 10, 2005 7:52 pm
by Chris Corbyn
Jenk wrote:This should be in the RegEx forum, as it is about... RegEx.

Code: Select all

preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(:[0-9]{1,5})?$/", $ipaddr);
:D You've been stricter on the port checking than you were on the actual IP :P

Code: Select all

/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
You can go far better than this though if needs be. Check code snippets on this forum ;)