search
HomeWeb Front-endJS TutorialPHP正则表达式入门教程(推荐)_php实例

思维导图

点击下图,可以看具体内容!

介绍

正则表达式,大家在开发中应该是经常用到,现在很多开发语言都有正则表达式的应用,比如javascript,java,.net,php等等,我今天就把我对正则表达式的理解跟大家唠唠,不当之处,请多多指教!

定位

我们什么时候使用正则表达式呢?不是所有的字符操作都用正则就好了,php在某些方面用正则反而影响效率。当我们遇到复杂文本数据的解析时候,用正则是比较好的选择。

优点

正则表达式在处理复杂字符操作的时候,可以提高工作效率,也在一定程度节省你的代码量。

缺点

我们在使用正则表达式的时候,复杂的正则表达式会加大代码的复杂度,让人很难理解。所以我们有的时候需要在正则表达式内部添加注释。

通用模式

¤ 定界符,通常使用 "/"做为定界符开始和结束,也可以使用"#"。

  什么时候使用"#"呢?一般是在你的字符串中有很多"/"字符的时候,因为正则的时候这种字符需要转义,比如uri。

使用"/"定界符的代码如下.

$regex = '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i';
$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

preg_match中的$matches[0]将包含与整个模式匹配的字符串。

使用"#"定界符的代码如下.这个时候对"/"就不转义!

$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';
$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

¤ 修饰符:用于改变正则表达式的行为。

我们看到的('/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html/i')中的最后一个"i"就是修饰符,表示忽略大小写,还有一个我们经常用到的是"x"表示忽略空格。

贡献代码:

$regex = '/HELLO/';
$str = 'hello word';
$matches = array();
if(preg_match($regex, $str, $matches)){
echo 'No i:Valid Successful!',"\n";
}
if(preg_match($regex.'i', $str, $matches)){
echo 'YES i:Valid Successful!',"\n";
}

¤ 字符域:[\w]用方括号扩起来的部分就是字符域。

¤ 限定符:如[\w]{3,5}或者[\w]*或者[\w]+这些[\w]后面的符号都表示限定符。现介绍具体意义。

{3,5}表示3到5个字符。{3,}超过3个字符,{,5}最多5个,{3}三个字符。

* 表示0到多个

+ 表示1到多个。

¤ 脱字符号

^:

> 放在字符域(如:[^\w])中表示否定(不包括的意思)——“反向选择”

> 放在表达式之前,表示以当前这个字符开始。(/^n/i,表示以n开头)。

注意,我们经常管"\"叫"跳脱字符"。用于转义一些特殊符号,如".","/"

通配符(lookarounds):断言某些字符串中某些字符的存在与否!

lookarounds分两种:lookaheads(正向预查 ?=)和lookbehinds(反向预查?

> 格式:

正向预查:(?=) 相对应的 (?!)表示否定意思

反向预查:(?

前后紧跟字符

$regex = '/(&#63;<=c)d(&#63;=e)/'; /* d 前面紧跟c, d 后面紧跟e*/
$str = 'abcdefgk';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

否定意义:

$regex = '/(&#63;<!c)d(&#63;!e)/'; /* d 前面不紧跟c, d 后面不紧跟e*/
$str = 'abcdefgk';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

>字符宽度:零

验证零字符代码

$regex = '/HE(&#63;=L)LO/i';
$str = 'HELLO';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

打印不出结果!

$regex = '/HE(&#63;=L)LLO/i';
$str = 'HELLO';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

能打印出结果!

说明:(?=L)意思是HE后面紧跟一个L字符。但是(?=L)本身不占字符,要与(L)区分,(L)本身占一个字符。

捕获数据

没有指明类型而进行的分组,将会被获取,供以后使用。

> 指明类型指的是通配符。所以只有圆括号起始位置没有问号的才能被捕捉。

> 在同一个表达式内的引用叫做反向引用。

> 调用格式: \编号(如\1)。

$regex = '/^(Chuanshanjia)[\w\s!]+\1$/'; 
$str = 'Chuanshanjia thank Chuanshanjia';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

> 避免捕获数据

格式:(?:pattern)

优点:将使有效反向引用数量保持在最小,代码更加、清楚。

>命名捕获组

格式:(?P) 调用方式 (?P=组名)

$regex = '/(&#63;P<author>chuanshanjia)[\s]Is[\s](&#63;P=author)/i';
$str = 'author:chuanshanjia Is chuanshanjia';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

运行结果

惰性匹配(记住:会进行两部操作,请看下面的原理部分)

  格式:限定符?

原理:"?":如果前面有限定符,会使用最小的数据。如“*”会取0个,而“+”会取1个,如过是{3,5}会取3个。

先看下面的两个代码:

代码1.

<&#63;php
$regex = '/heL*/i';
$str = 'heLLLLLLLLLLLLLLLL';
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

结果1.

代码2

<&#63;php
$regex = '/heL*&#63;/i';
$str = 'heLLLLLLLLLLLLLLLL';
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

结果2

代码3,使用“+”

<&#63;php
$regex = '/heL+&#63;/i';
$str = 'heLLLLLLLLLLLLLLLL';
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

结果3

代码4,使用{3,5}

<&#63;php
$regex = '/heL{3,10}&#63;/i';
$str = 'heLLLLLLLLLLLLLLLL';
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

结果4

正则表达式的注释

格式:(?# 注释内容)

用途:主要用于复杂的注释

贡献代码:是一个用于连接MYSQL数据库的正则表达式

$regex = '/
^host=(&#63;<!\.)([\d.]+)(&#63;!\.) (&#63;#主机地址)
\|
([\w!@#$%^&*()_+\-]+) (&#63;#用户名)
\|
([\w!@#$%^&*()_+\-]+) (&#63;#密码)
(&#63;!\|)$/ix';
$str = 'host=192.168.10.221|root|123456';
$matches = array();
if(preg_match($regex, $str, $matches)){
var_dump($matches);
}
echo "\n";

特殊字符

特殊字符 解释
* 0到多次
+ 1到多次还可以写成{1,}
? 0或1次
. 匹配除换行符外的所有单个的字符
\w [a-zA-Z0-9_]
\s 空白字符(空格,换行符,回车符)[\t\n\r]
\d [0-9]

以上所述是小编给大家介绍的PHP正则表达式入门教程的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

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
如何使用PHP正则表达式验证输入是否是IPv6地址如何使用PHP正则表达式验证输入是否是IPv6地址Jun 25, 2023 am 09:37 AM

IPv6是指InternetProtocolVersion6,是用于互联网通信的一种IP地址协议。IPv6地址是由128个比特位组成的数字,通常用8个16进制数分组表示。在PHP中,可以使用正则表达式来验证输入是否是IPv6地址,下面就介绍一下如何使用PHP正则表达式验证IPv6地址。第一步:了解IPv6地址的格式IPv6地址由8个16进制块组成,每个

如何用PHP正则表达式验证字符串是否为空如何用PHP正则表达式验证字符串是否为空Jun 24, 2023 am 08:46 AM

在PHP中,我们可以使用正则表达式来验证字符串是否为空。字符串为空的情况包括以下情况:字符串只包含空格。字符串长度为0。字符串为null或者未定义。接下来,我们将介绍如何使用PHP中的正则表达式来验证这些情况。正则表达式:s+这个正则表达式可以用来匹配只包含空格的字符串。其中s表示匹配空格,+表示匹配一个或多个。代码示例:functionisEmptySt

如何用PHP正则表达式验证电话号码格式如何用PHP正则表达式验证电话号码格式Jun 24, 2023 am 08:44 AM

在编写Web应用程序时,经常需要进行电话号码的验证。PHP中常用的方法是使用正则表达式来判断电话号码的格式是否正确。正则表达式是一个强大的工具,它可以帮助您在简洁的语句中确定某些模式。下面是在PHP中使用正则表达式来验证电话号码格式的示例。首先,让我们定义电话号码的通用格式。电话号码可以包含数字、括号、连字符和空格。一个标准的电话号码应该包含10个数字,前

如何用PHP正则表达式验证URL地址格式如何用PHP正则表达式验证URL地址格式Jun 24, 2023 am 09:51 AM

随着互联网的快速发展,URL地址已经成为了人们日常生活中不可或缺的一部分。在web开发中,为了保证用户输入的URL地址可以正确地被系统识别和使用,我们需要对其进行格式验证。本文将介绍如何使用PHP正则表达式来验证URL地址格式。一、URL地址的基本组成部分在了解如何验证URL地址格式之前,我们首先需要了解URL地址的基本组成部分。通常,一个标准的URL地址由

PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式Jun 24, 2023 pm 12:11 PM

身份证号码和护照号码是人们生活中常见的证件号码。在实现涉及到这些证件号码的功能时,经常需要对输入的号码进行格式验证,以确保其正确性。而在PHP中,使用正则表达式可以很好地实现这一功能,本文就介绍如何使用PHP正则表达式验证输入字符串是否为身份证号码或护照号码格式。一、身份证号码验证身份证号码是由18位数字和最后一位可能是字母(校验码)组成的,其格式如下:前6

如何在PHP中使用正则表达式验证是否是文件路径如何在PHP中使用正则表达式验证是否是文件路径Jun 24, 2023 am 10:18 AM

在PHP中,正则表达式是一种常用的字符串匹配和验证工具。在开发过程中,需要经常对输入的文件路径进行验证,确保其格式正确。本文将介绍如何使用正则表达式验证一个字符串是否是文件路径。首先,我们需要确定一个文件路径的基本格式。在Windows系统中,一个典型的文件路径是类似于“C:ProgramFilesPHPphp.exe”这样的格式。该路径分为以下几个部分:

如何用PHP正则表达式验证输入字符串是否为正确的身份证号码、护照号码或港澳通行证格式如何用PHP正则表达式验证输入字符串是否为正确的身份证号码、护照号码或港澳通行证格式Jun 24, 2023 am 10:36 AM

身份证、护照和港澳通行证号码都是重要的个人身份证明,为了保障个人信息安全,我们需要在系统中验证用户输入的证件号码是否符合规范格式。而PHP正则表达式是一个非常强大的工具,可以方便地实现这个目的。本文将介绍如何使用PHP正则表达式验证用户输入的身份证号码、护照号码和港澳通行证号码。一、身份证号码格式验证身份证号码是18位数字,在最后一位可能是数字或字母X。身份

如何使用PHP正则表达式验证特定长度的输入如何使用PHP正则表达式验证特定长度的输入Jun 24, 2023 am 10:17 AM

在开发Web应用程序时,经常需要验证用户输入是否符合特定的格式和长度要求。PHP正则表达式提供了一种强大的方法进行验证。本文将介绍如何使用PHP正则表达式验证特定长度的输入。确定输入的长度要求在开始编写正则表达式之前,需要确定输入的长度要求。例如,如果要求用户输入一个长度为8的密码,那么正则表达式应该匹配8个字符,而不是匹配大于等于8个字符的字符串。编写正则

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.

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.

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.