Commonly used regular expressions in PHP include phone number, email, username, mobile phone number, email, password, QQ number, ID number, spaces, letters, numbers, etc. These regular expression test codes are as follows. Let me sort it out.
Refer to N+1 regular summary:
Common regular matches:
代码如下 | 复制代码 |
((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[X0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3})) |
Matches Tencent QQ numbers: [1-9][0-9]{4,} Note: Tencent QQ numbers start from 10000
Price: /(^[-+]?[1-9]d*(.d{1,2})?$)|(^[-+]?[0]{1}(.d{1,2 })?$)/
Verify user password: "^[a-zA-Z]w{5,17}$" The correct format is: starts with a letter, has a length between 6 and 18, and can only contain characters, numbers and underscores.
China Postal Code:[1-9]{1}(d+){5}
Chinese mobile phone number: (86)*0*13d{9}
Mask:
The code is as follows | Copy code | ||||
|
Chinese ID number:
代码如下 | 复制代码 |
((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[X0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3})) |
The code is as follows | Copy code |
((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51 |52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9 ][0-3][0-9][0-9]{3}[X0-9])|([0-9]{2}[0|1][0-9][0-3] [0-9][0-9]{3})) |
Matches spaces/empty lines:
Regular expression matching empty lines: n[s| ]*r
Regular expression matching leading and trailing spaces: (^s*)|(s*$)
Regular expression to match Chinese characters: [u4e00-u9fa5]
Match numbers:
Only numbers can be entered: "^[0-9]*$"
Integer or decimal: ^[0-9]+.{0,1}[0-9]{0,2}$
Only numbers with at least n digits can be entered: "^d{n,}$".
Only m~n digits can be entered:. "^d{m,n}$"
Only numbers starting with zero and non-zero can be entered: "^(0|[1-9][0-9]*)$"
Only positive real numbers with two decimal places can be entered: "^[0-9]+(.[0-9]{2})?$"
You can only enter positive real numbers with 1~3 decimal places: "^[0-9]+(.[0-9]{1,3})?$"
Only non-zero positive integers can be entered: "^+?[1-9][0-9]*$"
Only non-zero negative integers can be entered: "^-[1-9][]0-9"*$
Only characters with a length of 3 can be entered: "^.{3}$"
Matching letters:
Only a string consisting of 26 English letters can be entered: "^[A-Za-z]+$"
You can only enter a string consisting of 26 uppercase English letters: "^[A-Z]+$"
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$"
You can only enter a string consisting of numbers and 26 English letters: "^[A-Za-z0-9]+$"
You can only enter a string consisting of numbers, 26 English letters or underscores: "^w+$"
Verify whether it contains characters such as ^%&',;=?$": "[^%&',;=?$x22]+"
Attached below are some of my own regular expression applications
The code is as follows | Copy code | ||||||||
function pregTP($test){
Phone number matching Phone number rules:Area code: 3 to 5 digits, most are four digits, Beijing (010) and Shanghai (021) are three digits, Tibet has some five digits, it can be wrapped in brackets or not If there is an area code wrapped in brackets, there can be 0 to 1 space between the area code and the number. If the area code is not wrapped in brackets, there can be two digits between the area code and the number or -
|
The code is as follows | Copy code |
function pregURL($test){ /** Matching url URL rules: Example Protocol://Domain name (www/tieba/baike...).Name.Suffix/File path/File name http://www.bKjia.c0m Protocol://Domain name (www/tieba/baike...).Name.Suffix/File path/File name?Parameters www.www.bKjia.c0m Protocol://Domain name (www/tieba/baike...).Name.Suffix/File path/File name/Parameters http://www.hzhuti.com Protocol: optional, consisting of uppercase and lowercase letters; if protocol is not written, it should not exist://, otherwise it must exist:// Domain name: must exist, consisting of uppercase and lowercase letters Name: must exist, alphanumeric Chinese characters Suffix: must exist, composed of uppercase and lowercase letters and . File path: optional, consisting of uppercase and lowercase letters and numbers File name: optional, consisting of uppercase and lowercase letters and numbers Parameters: optional, if they exist, they must start with ?, that is, if they exist, there must be corresponding parameter information starting with ? */ $rule = '/^(([a-zA-Z]+)(://))?([a-zA-Z]+).(w+).([w.]+)(/([ w]+)/?)*(/[a-zA-Z0-9]+.(w+))*(/([w]+)/?)*(?(w+=?[w]*)) *((&?w+=?[w]*))*$/'; preg_match($rule,$test,$result); return $result; } |
代码如下 | 复制代码 |
function pregPOS($test){ /** 匹配邮编 规则:六位数字,第一位不能为0 */ $rule ='/^[1-9]d{5}$/'; preg_match($rule,$test,$result); return $result; } |
代码如下 | 复制代码 |
function pregCh($test){ //utf8下匹配中文 $rule ='/([x{4e00}-x{9fa5}]){1}/u'; preg_match_all($rule,$test,$result); return $result; } |
代码如下
|
复制代码
|
||||
/** Matching time Rules: The form can be: Year-Month-Day Hour:Minute:Second Year-Month-Day Hour:Minute Year-month-day Year: four digits starting with 1 or 2 Month: 1 digit number from 1 to 9; two digits starting with 0 or 1, when the single digit starts with 0, it is a number from 1 to 9, when it starts with 1, the single digit is a number from 1 to 2 Day: 1 digit number from 1 to 9; two digits starting with 0 or 1 or 2 or 3. When the unit digit starts with 0, it is a number from 1 to 9. When it starts with 1 or 2, the units digit is 0 to 9. When the number starts with 3, the single digit is 0 or 1 Hours: one digit from 0 to 9; two digits starting with 0 or 1, the ones digit is 0 to 9; two digits starting with 2, the ones digit is 0-3 Minutes: one digit from 0 to 9; two digits starting from 0 to 5, the ones digit is 0 to 9; Minutes: one digit from 0 to 9; two digits starting from 0 to 5, each digit is 0 to 9 */ $rule ='/^(([1-2][0-9]{3}-)((([1-9])|(0[1-9])|(1[0-2]))-)((([1-9])|(0[1-9])|([1-2][0-9])|(3[0-1]))))( ((([0-9])|(([0-1][0-9])|(2[0-3]))):(([0-9])|([0-5][0-9]))(:(([0-9])|([0-5][0-9])))?))?$/'; preg_match($rule,$test,$result); return $result; } |