# 1.2 正则表达式

### 术语解释 <a href="#e6-9c-af-e8-af-ad-e8-a7-a3-e9-87-8a" id="e6-9c-af-e8-af-ad-e8-a7-a3-e9-87-8a"></a>

正则表达式是一种描述字符串结构的语法规则，是一个特定的格式化模式，可以匹配、替换、截取匹配的字符串。

* grep：最初是 ED 编辑器中的一条命令，用来显示文件中特定的内容，后来成为一个独立的工具 grep。
* egrep：贝尔实验室推出了egrep，意为“扩展的 grep”，这大大增强了正则表达式的能力。
* POSIX（Portable Operating System Interface of Vnix）

> The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems.

* Perl（Practical Extraction and Reporting Language）

> Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier.

* PCRE

> Perl Compatible Regular Expressions (PCRE) is a library written in C, which implements a regular expression engine, inspired by the capabilities of the Perl programming language. Philip Hazel started writing PCRE in summer 1997.\[3] PCRE's syntax is much more powerful and flexible than either of the POSIX regular expression flavors and than that of many other regular-expression libraries.
>
> A number of prominent open-source programs, such as the Apache and Nginx HTTP servers, and the PHP and R scripting languages, incorporate the PCRE library; proprietary software can do likewise, as the library is BSD-licensed.

### 语法规则 <a href="#e8-af-ad-e6-b3-95-e8-a7-84-e5-88-99" id="e8-af-ad-e6-b3-95-e8-a7-84-e5-88-99"></a>

一个完整的正则表达式由两部分构成，元字符和文本字符。元字符就是具有特殊含义的字符，如：“\*”，“？”。文本字符就是普通的文本，如字母和数字等。

PCRE 风格的正则表达式一般都放置在定界符“/”中间。

**行定位符(^和$)**

* "^"表示行的开始；
* "$"表示行的结尾；

**单词定界符(\b、\B)**

* "\b"表示要查找的字串为一个完整的单词；
* "\B"表示一个要查找的字串**不是**一个完整的单词；

**字符类(\[])**

只要匹配的字符出现在方括号内，即可表示匹配成功。

POSIX 和 PCRE 都使用了一些预定义字符类。POSIX 风格的预定义字符类举例：

* \[:digit:]十进制数字集合。等同于\[0-9]
* \[\[:alnum:]]字母和数字的集合。等同于\[a-zA-Z]
* \[\[:alpha:]]字母集合。等同于\[a-zA-Z]\
  ...

而 PCRE 的预定义字符类则使用反斜线来表示。

**连字符(-)**

连字符可以表示字符的范围。

如：\[a-zA-Z]

**排除字符(\[^])**

"^"在方括号中，表示排除的意思

**限定符(? \* + {n,m})**

对于重复出现的字符或字串，可以使用限定符来实现匹配。

* ？：匹配前面的字符零次或一次
* +：匹配前面的字符一次或多次
* \*：匹配前面的字符零次或多次
* {n}：匹配前面的字符n次
* {n,}：匹配前面的字符至少n次
* {n,m}：匹配前面的字符最少n次，最多m次

**点好字符(.)**

可以匹配出换行符外的任意一个字符。

**转义字符()**

将特殊字符（如：“，”、“？”、“、”等）变为普通字符。

**反斜线()**

除了做转义字符外，反斜线还有一些其他功能。

* 反斜线可以将一些不可打印的字符显示出来；
* 可以预定义字符集
* 可以定义断言，如：“\b”、“\B”

**括号字符(())**

* 小括号字符的第一个作用就是可以改变限定字符的作用范围，如“｜”、“\*”、“^”等；
* 小括号字符的第二个作用是分组，也就是子表达式。如：(.\[0-9]{1,3}){3}

**反向引用**

...

**模式修饰符**

...

### PHP 中的 PCRE — 正则表达式(兼容 Perl) <a href="#php-e4-b8-ad-e7-9a-84-pcre-e2-80-94-e6-ad-a3-e5-88-99-e8-a1-a8-e8-be-be-e5-bc-8f-e5-85-bc-e5-ae-b9-p" id="php-e4-b8-ad-e7-9a-84-pcre-e2-80-94-e6-ad-a3-e5-88-99-e8-a1-a8-e8-be-be-e5-bc-8f-e5-85-bc-e5-ae-b9-p"></a>

> <https://www.php.net/manual/zh/book.pcre.php>

* preg\_filter — 执行一个正则表达式搜索和替换
* preg\_grep — 返回匹配模式的数组条目
* preg\_last\_error — 返回最后一个PCRE正则执行产生的错误代码
* preg\_match\_all — 执行一个全局正则表达式匹配
* preg\_match — 执行匹配正则表达式
* preg\_quote — 转义正则表达式字符
* preg\_replace\_callback\_array — Perform a regular expression search and - replace using callbacks
* preg\_replace\_callback — 执行一个正则表达式搜索并且使用一个回调进行替换
* preg\_replace — 执行一个正则表达式的搜索和替换
* preg\_split — 通过一个正则表达式分隔字符串


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haxianhe.gitbook.io/php/di-yi-zhang-php-ji-chu/1.2-php-zheng-ze-biao-da-shi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
