Regular Expressions

Anyone who knows me well knows that I am Linux / Unix nut, a CLI nut, and that I LOVE regexs.

There are several regexes I find myself using relatively frequently at work, so I've decided to share them here, in case anyone else finds them useful.


Any engine should be able to use this to find Cisco IOS Version numbers in text:

[0-9]{2}\.[0-9]\([0-9]+[a-z]?\) ?([A-Z]+[0-9]?)?

This one will help you find an IP address in some text, and only valid IP addresses, NOT junk like 333.256.456.999 :


Here it is again, improved with non-capturing groups:


Sometimes, you only want to find the RFC1918 ip addresses in some text. These will do the trick:


Here's an easy one for MAC addresses that's forgiving of separators:

(?:[0-9A-Fa-f]{2}[-: ]){5}[0-9A-Fa-f]{2}


Now this stuff is interesting. Want to make sure you are looking at valid credit card numbers? Maybe for some DLP application, or preliminary e-commerce validation? 

 VISA - 4[0-5](?:[0-9][- ]?){11}(?:[0-9]{3})?
MC - 5[1-5](?:[0-9][- ]?){14}
Amex - 3[47][0-9]{2}[- ]?[0-9]{6}[- ]?[0-9]{5}
Diner's Club - 3(?:0[0-5]|[68][0-9])[0-9]{11}


This one is a doozy, but again, great for DLP situations. It matches only VALID US Social Security Numbers:

(?!000)(?:[0-6]\d{2}|7(?:[0-6]\d|7[0-2]))([- ]?)(?!00)\d\d\1(?!0000)\d{4}


And here are some other ones I've found handy in the past:

Validate password is 8 to 15 characters w/ 1 upper, 1 lower, 1 digit:


Match an HTML tag (don't forget to escape /'s in some cases):


Comify big numbers in perl:


Look for valid URLs:



If I cook up any others, I'll add them here.