Mar
24

Useful regular expressions

RegEx          Trackback

Phone Numbers:
(999) 999-9999
999-999-9999
999.999.9999

[\(.]?[2-9]\d\d[\).]?[ -]?[2-9]\d\d[-.]\d{4}

USA ZIP Codes
12345
12345-1234

\d{5}(-\d{4})?

Canadian postal codes
A1B 2C3

[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d

UK postal codes
A12 3BC

[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}

US Social Security Number
123-45-6789

\d{3}-\d{2}-\d{4}

IP Address
123.234.123.234

(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))

URL
http://www.zemoon.comhttp://localhost:80

https?://[-\w.]+(:\d+)?(/([\w/_.]*)?)?


Complete URL
http://www.zemoon.com/index.htm
http://blog.zemoon.com/index.php?p=1

https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?

Email Address
someone@something.com

(\w+\.)*\w+@(\w+\.)+[A-Za-z]+

HTML Comments
<!– This is comment –>

C style comments

//.*

Credit card numbers
Mastercard

5[1-5]\d{14}


Visa

4\d{12}(\d{3})?


AmericanExpress

3[47]\d{13}


Discovery

6011\d{14}


Diners

(30[0-5]|36\d|38\d)\d{11}


All credit cards

(5[1-5]\d{14})|(4\d{12}(\d{3})?)|(3[47]\d{13})|(6011\d{14})|((30[0-5]|36\d|38\d)\d{11})


top