IP Address regular expression

string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
Author Paul Hayman

Paul is the COO of kwiboo ltd and has more than 20 years IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services.

Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.


Shatheesh said:

Thanks... It works...

15/Nov/2006 11:04 AM

kamal said:

Thanks for the Regex ..

Could you also include regular expression strings for other validations too,generally not found over net.

Thanks in advance!

05/Jul/2007 08:11 AM

phayman said:

05/Jul/2007 08:59 AM

Paul said:

It appears that the regex does not consider Ipv6

22/Jan/2008 19:55 PM

masterik said:

I've found another one at http://www.regular-expressions.info/examples.html:


14/Mar/2008 10:30 AM

Wieland K said:

thank you! works great

20/May/2008 23:37 PM

steve said:

Thanks. You saved me a lot of time and trouble!

11/Jul/2008 13:22 PM

a++ said:

try this:


22/Apr/2009 10:24 AM

immeëmosol said:

Wouldn't your expression allow for: ?

I do this to check if I am on my home-server (PHP):

preg_match('/^127(?:\\.(?:25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]?)){2}\\.[1-9][0-9]{0,2}$/' , $SERVER['SERVERADDR'])

Regexes are good stuff.

06/May/2009 12:53 PM

Haider said:

Thanks Great working...

16/Jul/2009 07:36 AM

haidiva said:

HI , I want regular expre.. for ip address with wild card support .

Can any body help me on this .

03/Sep/2010 06:59 AM

ffatheranderson said:

Hi i won't to fix the "zero" and "255" ending ip address allowing

Bellow the rgex that don't allow addresses like "x.x.x.255" or "x.x.x.0" it is good for IP address as is.


And this regex (was sended by steve(thanx you very useful)) good for check network address or multicast addressing like x.x.x.255" or "x.x.x.0.


26/Apr/2011 17:20 PM

Aiswaryam said:

Hello Paul,

Can you please tell me how to add regex express to whitelist pw-admin folder access for a range of ip address in nginx for our website http://www.freezonal.com/ which is using nginx?

30/Sep/2011 19:00 PM

narcisisimus said:

Hey, but all the possibilities don't cover xx.xxx.xxx.xx ? why?

06/Feb/2012 12:03 PM

Nguy?n Trung Tín said:

ex: ip:port xx.xx.xx.xx:xxxx how to ?

10/Mar/2012 15:53 PM

Add Comment


Your comment has been received and will be shown once it passes moderation.