PHP核心技术与最佳实践之正则表达式婚配规则
PHP核心技术与最佳实践之正则表达式匹配规则
PHP核心技术与最佳实践之正则表达式匹配规则
本文介绍几种常用的匹配规则。
1. 字符组
查找数字、字母、空白很简单,因为已经有了对应这些集合的元字符,但是如果匹配没有预定义元字符的字符集合,方法很简单, 就是在方括号内列出它们。
例如:[aeiou]匹配任何一个英文元音字母,[.*?]匹配标点中的一个。注意此时方括号内的元字符失去了特殊意义。
也可以指定字符范围,例如[0-9]的含义和\d完全一致:代表一位数字;同理[a-zA-Z0-9]等同于\w;
字符组很简单,但是一定要弄清楚字符组中什么时候需要转义。
2. 转义
如果想要查找或匹配元字符本身,比如查找*、?等就出现问题:没办法指定,因为它们会被解释成别的意思。这时就需要\来取消这些字符的特殊意义。这叫转义。
在PHP中使用反斜杠(\)表示转义,\Q和\E也可以在模式中忽略正则表达式的元字符。比如:
\d +\Q.$.\E$
以上表达式先匹配一个或多个数字,紧接着一个.点号,然后一个$,再然后一个.点号,最终是字符串末尾。也就是说\Q和\E中的元字符会被作为普通字符来匹配。
3. 反义
有些时候,查找的字符不属于某个字符类,或者表达式和已知定义相反,(比如除了数字以外其他字符),这时需要用到反义。
常用反义:
常用反义 |
描述 |
\W |
匹配任意不是字母、数字、下划线、汉字的字符 |
\S |
匹配任意不是空白符的字符 |
\D |
匹配任意非数字的字符 |
\B |
匹配不是单词开头或结束的位置 |
[^x] |
匹配除了x以外的任意字符 |
反义有一个比较明显的特征,就是和一些已知元字符相反,并且为大写形式。比如”\D”就表示非数字。
1) 不包含空白符的字符串
\S+
2) 用尖括号扩起来、以a开头的字符串:
提示:
“^”这里是非的意思,不是开头的。如何区分?
表示开头的 ^只能用在正则表达式的最前端,而表示取反的^只能用在字符组中,即只在中括号内出现。
注意:
不要随意使用反义,因为反义无形中扩大范围,而使自己没有考虑到。
4. 分支
分支就是存在多种可能的匹配情况。
(c|h|f|to|)cat
其中括号里的表达式将视为一个整体,分支条件指有几种规则,无论满足哪一种规则都能匹配,具体方法是使用“|”方法把不同的规则分隔开。
5. 分组
重复单个字符只需直接在字符后面加上限定符,但如果想重复多个字符?
常用的分组语法:
类别 |
语法 |
描述 |
捕获 |
(exp) |
匹配exp,并捕获文本到自动命名的组里 |
(? |
匹配exp,并捕获文本到name的组里 |
|
(?:exp) |
匹配exp,不捕获匹配的文本 |
|
零宽断言 |
(?=exp) |
匹配exp前面的位置 |
(? |
匹配exp后面的位置 |
|
(?!exp) |
匹配后面不是exp的位置 |
|
(? |
匹配前面不是exp的位置 |
|
注释 |
(?#comment) |
注释,不对正则有任何影响 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
