search
Homephp教程php手册PHP下常用正则表达式整理

PHP下常用正则表达式整理

Jun 13, 2016 pm 12:13 PM
phpDownCommonly usedtidyregularexpression

--------------------------------------------------------- 正则收藏

手机号码:
$mode = "/^1[358]\d{9}/";

邮箱地址:
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";

---------------------------------------------------------- 正则基础

$mode = "/^1[358]\d{9}/i";
匹配模块必须以 / / 开始和结束,第二个 / 后可以加模式修正符

原子
①a-z A-Z _ 0-9 //最常见的字符
②(abc) //用圆括号括起来起来的单元符号
③[abcs] [^abd] //用方括号括起来的原子表,
原子表中的^代表排除或相反内容

\d 包含所有数字[0-9]
\D 除所有数字外[^0-9]
\w 包含所有英文字符[a-zA-Z_0-9]
\W 除所有英文字符外[^a-zA-Z_0-9]
\s 包含空白区域如回车、换行、分页等 [\f\n\r]

元字符
* 匹配前一个内容的0次1次或多次
+ 1次或多次
? 0次或1次
. 代表任意一个字符(除了回车换行)
| 相当与php的 || (“或”的意思)
^ 强制匹配字符串首部内容
$ 强制匹配字符串尾部内容
[^abc] 匹配除了a或b或c之外的内容
\b 匹配单词边界,边界可以是空格或者特殊符号
\B 匹配除带单词边界以外的内容
{m} 匹配前一个内容的重复次数为M次
{m,} 匹配前一个内容的重复次数大于等于M次
{m,n} 匹配前一个内容的重复次数M次到N次
( ) 整体匹配,并放入内存,可使用\\1 或 \\2 …依次获取

优先级:依次降低
( ) 圆括号因为是内存处理所以最高
* ? + { } 重复匹配内容其次
^ $ \b 边界处理第三
| 条件处理第四
最后按照运算顺序计算匹配

常用修正符: $mode = "/正则/U";
i 正则内容在匹配时候不区分大小写(默认是区分的)
m 在匹配首内容或者尾内容时候采用多行识别匹配
S 将回车转化为空格
x 忽略正则中的空白
A 强制从头开始匹配
D 强制$匹配尾部无任何内容 \n
U 禁止贪婪匹配,只跟踪到最近的一个匹配符并结束,
常用在采集程序上的正则表达式

应用
preg_match_all ( string pattern, string subject, array matches [, int flags] )
截取比较详细的内容,采集网页,分析文本
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
提示 1、替换内容可以是一个正则也可以是数组正则
2、替换内容可以通过修正符e来解决替换执行内容
preg_split ( string pattern, string subject [, int limit [, int flags]] )
通过正则表达式来切割相关内容,类似之前学过的explode切割函数,但explode
只能按照一种方式切割有局限性。
------------------------------------------------- 调试代码
[code]
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";
$str = "a12345@jb51.net";
echo $str.'


';
if(preg_match($mode, $str, $arr)){
echo 'succeed -- '.$arr[0];
}else{
echo 'failed';
}
?>
[code]
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.