正規表示式

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

正規表示式

文章 yehlu »

http://www.virtuosimedia.com/tutorials/ ... xpressions
37 Tested PHP, Perl, and JavaScript Regular Expressions

Written by Benjamin / September 22, 2009

*
*
*

Facebook Reddit Technorati Email
Delicious Stumble Digg More...

*
*

A regular expression, also called regex or regexp for short, is simply a piece of code that matches a pattern. Mastering regular expressions can be a difficult chore, and if you don't need them all of the time, the syntax is tricky enough to make the task frustrating or slow as you will constantly need to use a reference sheet.

In order to save you time, I've compiled a list of PHP, Perl, and JavaScript regular expressions for common use cases that have been tested and are ready to go. This isn't a regular expression tutorial or even a reference; you can think of it more as a cheatsheet for when you just need the regex but don't want to put a lot of time into relearning regular expressions.

If you're looking for regex tutorials or regex resources, you can find them at the end of the page as well as some additional regex resources.

Perl and PHP Regular Expressions

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.
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})*$/'

1. //All major credit cards regex
2. '/^(?: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]*$/'

1. //Alpha-numeric characters only
2. '/^[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 ]*$/'

1. //Alpha-numeric characters with spaces only
2. '/^[a-zA-Z0-9 ]*$/'

Alphabetic Characters

This regex will test for alphabetic characters only (upper and lowercase).

//Alphabetic characters only
'/^[a-zA-Z]*$/'

1. //Alphabetic characters only
2. '/^[a-zA-Z]*$/'

American Express Credit Card

Verify Amex credit cards with this regexp.

//Amex credit card regex
'/^(3[47][0-9]{13})*$/'

1. //Amex credit card regex
2. '/^(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}))*$/'

1. //Australian Postal Codes
2. '/^((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])*$/'

1. //Canadian Postal Codes
2. '/^([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)*$/'

1. //Canadian Province Abbreviations
2. '/^(?: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})*$/'

1. //Date (MM/DD/YYYY)
2. '/^((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]))*$#'

1. //Date (YYYY/MM/DD)
2. '#^((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]*$/'

1. //Digits only
2. '/^[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})*$/'

1. //Diner's Club credit card regex
2. '/^(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})*$/'

1. //Email regex
2. '/^([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]?))*$/'

1. //IP address regex
2. '/^((?:(?: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])*$/'

1. //Lowercase letters only
2. '/^([a-z])*$/'

MasterCard Credit Card

Verify MasterCard credit card numbers with this regex.

//MasterCard credit card numbers
'/^(5[1-5][0-9]{14})*$/'

1. //MasterCard credit card numbers
2. '/^(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]))^.*$/'

1. //Password regex
2. '/^(?=^.{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})+)*$/'

1. //Phone number regex
2. '/^((([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})*$/'

1. //SSN regex
2. '/^([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})*$/'

1. //UK Postal Codes regex
2. '/^([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])*$/'

1. //Uppercase letters only
2. '/^([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]\/+=%&_\.~?\-]*))*$/'

1. //URL regex
2. '/^(((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])*$/'

1. //US States regex
2. '/^(?: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})?)*$/'

1. //US ZIP Codes regex
2. '/^([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})?)*$/'

1. //Visa credit card numbers
2. '/^(4[0-9]{12}(?:[0-9]{3})?)*$/'

JavaScript Regular Expressions
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.
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})$'

1. //All major credit cards JavaScript regex
2. '^(?: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]+$'

1. //JavaScript alpha-numeric characters only
2. '^[a-zA-Z0-9]+$'

Alphabetic Characters

This regex will test for alphabetic characters only (upper and lowercase).

//JavaScript Alphabetic characters only
'^[a-zA-Z]+$'

1. //JavaScript Alphabetic characters only
2. '^[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]$'

1. //JavaScript Canadian Postal Codes
2. '^[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}$'

1. //JavaScript Date (MM/DD/YYYY)
2. '^(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])$'

1. //JavaScript Date (YYYY/MM/DD)
2. '^(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]+$'

1. //JavaScript digits only
2. '^[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}$'

1. //JavaScript email regex
2. '^[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]))^.*"

1. //JavaScript Password regex
2. "(?=^.{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})+$'

1. //JavaScript phone number regex
2. '^(([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]/+=%&_\.~?\-]*)$'

1. //JavaScript URL regex
2. '^((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})?$'

1. //JavaScript US ZIP Codes regex
2. '^[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.

http://regexlib.com/
回覆文章

回到「PHP」