regex for an ip

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

regex for an ip

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to Regex.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Post Reply