PHP common regular matching expression study notes_PHP tutorial

WBOY
Release: 2016-07-13 10:48:46
Original
1002 people have browsed it

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
 代码如下 复制代码

/^(254|252|248|240|224|192|128|0).0.0.0$|^(255.(254|252|248|240|224|192|128|0).0.0)$|^(255.255.(254|252|248|240|224|192|128|0).0)$|^(255.255.255.(254|252|248|240|224|192|128|0|255))$/

/^(254|252|248|240|224|192|128|0).0.0.0$|^(255.(254|252|248|240|224|192|128|0). 0.0)$|^(255.255.(254|252|248|240|224|192|128|0).0)$|^(255.255.255.(254|252|248|240|224|192|128 |0|255))$/


China phone number (including mobile and landline): ((d{3,4})|d{3,4}-|s)?d{7,14} Regular expression "((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*" (matching form such as: 0511-4405222 or 021-87888822 Or 021-44055520-555 or (0511)4405222)

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){
 代码如下 复制代码
 function pregTP($test){
  /**
  电话号码匹配
  电话号码规则:
   区号:3到5位,大部分都是四位,北京(010)和上海市(021)三位,西藏有部分五位,可以包裹在括号内也可以没有
   如果有区号由括号包裹,则在区号和号码之间可以有0到1个空格,如果区号没有由括号包裹,则区号和号码之间可以有两位长度的 或者-
   号码:7到8位的数字
   例如:(010) 12345678  或者 (010)12345678 或者 010  12345678 或者 010--12345678
  */
  $rule = '/^((((010)|(021)|(0d{3,4})))( ?)([0-9]{7,8}))|((010|021|0d{3,4}))([- ]{1,2})([0-9]{7,8})$/A';
  preg_match($rule,$test,$result);
  return $result;
 }
/**

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 -
 代码如下 复制代码
function pregURL($test){
  /**
   匹配url
   url规则:
    例
    协议://域名(www/tieba/baike...).名称.后缀/文件路径/文件名
    http://www.bKjia.c0m
    协议://域名(www/tieba/baike...).名称.后缀/文件路径/文件名?参数
    www.www.bKjia.c0m
    协议://域名(www/tieba/baike...).名称.后缀/文件路径/文件名/参数
    http://www.hzhuti.com
    
    协议:可有可无,由大小写字母组成;不写协议则不应存在://,否则必须存在://
    域名:必须存在,由大小写字母组成
    名称:必须存在,字母数字汉字
    后缀:必须存在,大小写字母和.组成
    文件路径:可有可无,由大小写字母和数字组成
    文件名:可有可无,由大小写字母和数字组成
    参数:可有可无,存在则必须由?开头,即存在?开头就必须有相应的参数信息
  */
  $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;
 }
Number: 7 to 8 digits For example: (010) 12345678 or (010)12345678 or 010 12345678 or 010--12345678 ​*/ $rule = '/^((((010)|(021)|(0d{3,4})))( ?)([0-9]{7,8}))|((010|021| 0d{3,4}))([- ]{1,2})([0-9]{7,8})$/A'; preg_match($rule,$test,$result); return $result; }
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;
 }
 代码如下
 代码如下 复制代码

function pregTI($test){
  /**
  匹配时间
   规则:
    形式可以为:
    年-月-日 小时:分钟:秒
    年-月-日 小时:分钟
    年-月-日
    年:1或2开头的四位数
    月:1位1到9的数;0或1开头的两位数,0开头的时候个位数是1到9的数,1开头的时候个位数是1到2的数
    日:1位1到9的数;0或1或2或3开头的两位数,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
  */
  $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;
 }

复制代码

 代码如下 复制代码
function pregIP($test){
  /**
  匹配ip
  规则:
   **1.**2.**3.**4
   **1可以是一位的 1-9,两位的01-99,三位的001-255
   **2和**3可以是一位的0-9,两位的00-99,三位的000-255
   **4可以是一位的 1-9,两位的01-99,三位的001-255
   四个参数必须存在
  */
  $rule = '/^((([1-9])|((0[1-9])|([1-9][0-9]))|((00[1-9])|(0[1-9][0-9])|((1[0-9]{2})|(2[0-4][0-9])|(25[0-5])))).)((([0-9]{1,2})|(([0-1][0-9]{2})|(2[0-4][0-9])|(25[0-5]))).){2}(([1-9])|((0[1-9])|([1-9][0-9]))|(00[1-9])|(0[1-9][0-9])|((1[0-9]{2})|(2[0-4][0-9])|(25[0-5])))$/';
  preg_match($rule,$test,$result);
  return $result;
 }
function pregTI($test){
  /**
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;
 }
 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632747.htmlTechArticleCommonly used regular expressions in php include phone number, email, username, mobile phone number, email, Password, QQ number, ID number, spaces, letters, numbers, etc. Regular expressions like this...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!