Collection of useful Regex patterns

Here is a collection of useful Regex patterns that I have either come up with or have found on the internet. Enjoy.

 

Legend:

^[A-Z]+ # starts with at least one capital letter
s? # followed by an optional space
d+ # followed by one or more digits /x
^d+ # starts with one or more digits
. # followed by a single decimal /x
s # and a space /x
d{4} # then four numbers - year
- # connected with a dash

To filter a string to make sure it's a valid ip address this regexp is quite useful.
/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]).){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/

And this one for mac addresses
^([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}$

Extract web address from a string
((?:http|ftp)s?://)?(((([d]+.)+){3}[d]+(/[w./]+)?)|([a-z]w*((.w+)+){2,})([/][w.~]*)*)

matches a number: interger or float, scientific notation or plain, plus or minus...
[-+]?(?:b[0-9]+(?:.[0-9]*)?|.[0-9]+b)(?:[eE][-+]?[0-9]+b)?

Valid Phone Number Validation that allows extensions and virtually all the common ways to list a (US) number:
/^[01]?[-s.]?(?[2-9][0-9]{2})?([-s.]|(s-s))?[0-9]{3}([-s.]|(s-s))?[0-9]{4}s?(([xX].?|(ext|EXT).?|s)?s?(?<![0-9])[0-9]{1,4})?$/

SSN
^(?!000)([0-6]d{2}|7([0-6]d|7[012]))([ -]?)(?!00)dd3(?!0000)d{4}$

 


MKPortal C1.2.1 ©2003-2008 mkportal.it
Page generated in 0.0091 seconds with 10 queries