Perl and PHP Regular Expressions
When using PHP you can use the following built-in functions:
PHP regexes are based on the PCRE (Perl-Compatible Regular Expressions), so any regexp that works for one should be compatible with the other or any other language that makes use of the PCRE format. Here are some commonly needed regular expressions for both PHP and Perl. Each regex will be in string format and will include delimiters.
Perl and PHP Regex PCRE
Name | Description | Regex name | Regular Expression PCRE |
---|---|---|---|
All Major Credit Cards | This regular expression will validate all major credit cards: American Express (Amex), Discover, Mastercard, and Visa. | All major credit cards regex |
'/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|622((12[6-9]|1[3-9][0-9])|([2-8][0-9][0-9])|(9(([0-1][0-9])|(2[0-5]))))[0-9]{10}|64[4-9][0-9]{13}|65[0-9]{14}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})*$/' |
Alpha-Numeric Characters | Test for alpha-numeric characters with this regexp. | Alpha-numeric characters only |
'/^[a-zA-Z0-9]*$/' |
Alpha-Numeric Characters With Spaces | Test for alpha-numeric characters and spaces with this regexp. | Alpha-numeric characters with spaces only |
'/^[a-zA-Z0-9 ]*$/' |
Alphabetic Characters | This regex will test for alphabetic characters only (upper and lowercase). | Alphabetic characters only |
'/^[a-zA-Z]*$/' |
American Express Credit Card | Verify Amex credit cards with this regexp. | Amex credit card regex |
'/^(3[47][0-9]{13})*$/' |
Australian Postal Codes | If you need to verify Australian Postal Codes, use this regular expression. | Australian Postal Codes |
'/^((0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2}))*$/' |
Canadian Postal Codes | Tests for valid Canadian Postal Codes. | Canadian Postal Codes |
'/^([ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9])*$/' |
Canadian Provinces | Evaluate Canadian province abbreviations with this regular expression. | Canadian Province Abbreviations |
'/^(?:AB|BC|MB|N[BLTSU]|ON|PE|QC|SK|YT)*$/' |
Date (MM/DD/YYYY) | Validate the calendar date in MM/DD/YYYY format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099. | Date (MM/DD/YYYY) |
'/^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/' |
Date (YYYY/MM/DD) | Validate the calendar date in YYYY/MM/DD format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099. | Date (YYYY/MM/DD) |
'#^((19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01]))*$#' |
Digits | This regex will test for digits (whole numbers). | Digits only |
'/^[0-9]*$/' |
Diner's Club Credit Card | Test and verify Diner's Club credit card numbers with this regexp. | Diner's Club credit card regex |
'/^(3(?:0[0-5]|[68][0-9])[0-9]{11})*$/' |
Emails | This email regex is not fully RFC5322-compliant, but it will validate most common email address formats correctly. | Email regex |
'/^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})*$/' |
IP Addresses | Test IP Addresses with this regular expression. | IP address regex |
'/^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/' |
Lowercase Alphabetic Characters | This regex will test for lowercase letters. | Lowercase letters only |
'/^([a-z])*$/' |
MasterCard Credit Card | Verify MasterCard credit card numbers with this regex. | MasterCard credit card numbers |
'/^(5[1-5][0-9]{14})*$/' |
Passwords | Test for a strong password with this regex. The password must contain one lowercase letter, one uppercase letter, one number, and be at least 6 characters long. | Password regex |
'/^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/' |
Phone Numbers (North American) | This regex will validate a 10-digit North American telephone number. Separators are not required, but can include spaces, hyphens, or periods. Parentheses around the area code are also optional. | Phone number regex |
'/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/' |
Social Security Numbers | If you need to validate US Social Security Numbers, use this regular expression | SSN regex |
'/^([0-9]{3}[-]*[0-9]{2}[-]*[0-9]{4})*$/' |
UK Postal Codes | This regexp verifies UK Postal Codes. | UK Postal Codes regex |
'/^([A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2})*$/' |
Uppercase Alphabetic Characters | This regex will test for uppercase letters. | Uppercase letters only |
'/^([A-Z])*$/' |
URLs | This URL regex will validate most common URL formats correctly. | URL regex |
'/^(((http|https|ftp)://)?([[a-zA-Z0-9]-.])+(.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]/+=%&_.~?-]*))*$/' |
US States | Validate all 2-letter US State abbreviates with this regular expression. | US States regex |
'/^(?:A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])*$/' |
US ZIP Codes | This regexp verifies US ZIP Codes, with an optional 4 number ZIP code extension. | US ZIP Codes regex |
'/^([0-9]{5}(?:-[0-9]{4})?)*$/' |
Visa Credit Card | Verify Visa credit card numbers with this regex. | Visa credit card numbers |
'/^(4[0-9]{12}(?:[0-9]{3})?)*$/' |
JavaScript Regular Expressions
The JavaScript version of regex is a slightly different flavor than the PCRE variety, so I've included those regexes in a separate section.
Name | Description | Regex name | Regular Expression PCRE |
---|---|---|---|
All Major Credit Cards | This regular expression will validate all major credit cards: American Express (Amex), Discover, Mastercard, and Visa. Note that it is not quite as precise as its counterpart Perl and PHP regex. | All major credit cards JavaScript regex |
'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$' |
Alpha-Numeric Characters | Test for alpha-numeric characters with this regexp. | JavaScript alpha-numeric characters only |
'^[a-zA-Z0-9]+$' |
Alphabetic Characters | This regex will test for alphabetic characters only (upper and lowercase). | JavaScript Alphabetic characters only |
'^[a-zA-Z]+$' |
Canadian Postal Codes | Tests for valid Canadian Postal Codes. | JavaScript Canadian Postal Codes |
'^[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]$' |
Date (MM/DD/YYYY) | Validate the calendar date in MM/DD/YYYY format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099. | JavaScript Date (MM/DD/YYYY) |
'^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}$' |
Date (YYYY/MM/DD) | Validate the calendar date in YYYY/MM/DD format with this regex. Optional separators are spaces, hyphens, forward slashes, and periods. The year is limited between 1900 and 2099. | JavaScript Date (YYYY/MM/DD) |
'^(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])$' |
Digits | This regex will test for digits (whole numbers). | JavaScript digits only |
'^[0-9]+$' |
Emails | This email regex is not fully RFC5322-compliant, but it will validate most common email address formats correctly. | JavaScript email regex |
'^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$' |
Passwords | Test for a strong password with this regex. The password must contain one lowercase letter, one uppercase letter, one number, and be at least 6 characters long. | JavaScript Password regex |
'(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*' |
Phone Numbers (North American) | This regex will validate a 10-digit North American telephone number. Separators are not required, but can include spaces, hyphens, or periods. Parentheses around the area code are also optional. | JavaScript phone number regex |
'^(([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+$' |
URLs | This URL regex will validate most common URL formats correctly. | JavaScript URL regex |
'^((http|https|ftp)://)?([[a-zA-Z0-9]-.])+(.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]/+=%&_.~?-]*)$' |
US ZIP Codes | This regexp verifies US ZIP Codes, with an optional 4 number ZIP code extension. | JavaScript US ZIP Codes regex |
'^[0-9]{5}(?:-[0-9]{4})?$' |
Regex Tutorials
While this page doesn't go in depth on how to learn regular expressions, I will point you to some tutorials so that if you need to modify any of the above regexes or create your own, you'll be able to do so.
- Regex Crash Course (Part 1)
- Regex Crash Course (Part 2)
- Using Regular Expressions With PHP
- Perl Text Patterns For Search And Replace
- Using Regular Expressions With The Microsoft .NET Framework
- Python's re Module
- Using Regular Expressions with Ruby
- Using Regular Expressions with JavaScript and ActionScript
- Regular Expressions - A Simple User's Guide
- Mastering Regular Expressions (from Google Books)
Regex Resources And Reference Sheets
There are a number of different regex cheat sheets, libraries, testers, and other resources around the web. Here are just a few of them.
- Regular Expression Basic Syntax Reference
- REGULAR EXPRESSIONS CHEAT SHEET (V2)
- RegExLib.com Regular Expression Cheat Sheet (.NET)
- RegEx Library
- RegEx Online Tester
- Regex Useful Resources
- Regular Expression Generator
Sources
Thank you to Benjamin Kuker from Virtuosi Media for putting together the original list which I have made into a table format and updated!