|
|
|
|
@ -35,7 +35,7 @@ PHP提供了丰富的正则表达式函数,主要用于模式匹配、替换
|
|
|
|
|
示例:提取HTML中的链接
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$html = '<a href="link1.html">Link1</a><a href="link2.html">Link2</a>';
|
|
|
|
|
$html = '<a href="link1.html">Link1</a><a href="link2.html">Link2</a>';
|
|
|
|
|
preg_match_all('/<a href="(.*?)">(.*?)<\/a>/', $html, $matches);
|
|
|
|
|
// $matches[1] = ["link1.html", "link2.html"], $matches[2] = ["Link1", "Link2"]
|
|
|
|
|
```
|
|
|
|
|
@ -47,7 +47,7 @@ PHP提供了丰富的正则表达式函数,主要用于模式匹配、替换
|
|
|
|
|
示例:脱敏手机号
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$text = "用户电话:123-4567-8901";
|
|
|
|
|
$text = "用户电话:123-4567-8901";
|
|
|
|
|
$masked = preg_replace('/(\d{3})-(\d{4})-\d{4}/', '$1-****-$2', $text);
|
|
|
|
|
// 输出 "用户电话:123-****-4567"
|
|
|
|
|
```
|
|
|
|
|
@ -57,7 +57,7 @@ PHP提供了丰富的正则表达式函数,主要用于模式匹配、替换
|
|
|
|
|
示例:年份递增
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$text = "April fools day is 04/01/2002";
|
|
|
|
|
$text = "April fools day is 04/01/2002";
|
|
|
|
|
echo preg_replace_callback(
|
|
|
|
|
"|(\d{2}/\d{2}/)(\d{4})|",
|
|
|
|
|
function($m) { return $m[1] . ($m[2] + 1); },
|
|
|
|
|
@ -75,7 +75,7 @@ PHP提供了丰富的正则表达式函数,主要用于模式匹配、替换
|
|
|
|
|
示例:分割CSV
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$csv = "apple, orange; banana|pear";
|
|
|
|
|
$csv = "apple, orange; banana|pear";
|
|
|
|
|
$fruits = preg_split('/[,;|]/', $csv); // ["apple", "orange", "banana", "pear"]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -84,7 +84,7 @@ PHP提供了丰富的正则表达式函数,主要用于模式匹配、替换
|
|
|
|
|
示例:筛选有效邮箱
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$emails = ["test@qq.com", "invalid-email", "user@example.com"];
|
|
|
|
|
$emails = ["test@qq.com", "invalid-email", "user@example.com"];
|
|
|
|
|
$valid = preg_grep('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $emails);
|
|
|
|
|
// 输出 ["test@qq.com", "user@example.com"]
|
|
|
|
|
```
|
|
|
|
|
@ -102,11 +102,9 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- **元字符**:需转义后匹配(如 `\.` 匹配点号 `.`,`\*` 匹配星号 `*`)。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php
|
|
|
|
|
|
|
|
|
|
preg_match('/\.com/', 'example.com'); // 匹配 ".com"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- **特殊字符类**:
|
|
|
|
|
|
|
|
|
|
- `\d`:匹配数字(等价于 `[0-9]`)。
|
|
|
|
|
@ -135,7 +133,7 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- 非贪婪模式(在量词后加 `?`),如 `.*?` 匹配最短可能。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php$text = "<div>content</div>";
|
|
|
|
|
$text = "<div>content</div>";
|
|
|
|
|
preg_match('/<div>(.*?)<\/div>/', $text, $matches); // $matches[1] = "content"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -149,7 +147,7 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- `$`:匹配字符串结尾(多行模式下匹配行尾)。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
phppreg_match('/^Hello/', 'Hello world'); // 匹配成功
|
|
|
|
|
preg_match('/^Hello/', 'Hello world'); // 匹配成功
|
|
|
|
|
preg_match('/world$/', 'Hello world'); // 匹配成功
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -159,7 +157,7 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- `\B`:匹配非单词边界;可以认为是 `\b`的反义。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
phppreg_match('/\bcat\b/', 'The cat sat'); // 匹配 "cat"
|
|
|
|
|
preg_match('/\bcat\b/', 'The cat sat'); // 匹配 "cat"
|
|
|
|
|
preg_match('/\bcat\b/', 'category'); // 不匹配
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -170,22 +168,20 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- **捕获组**:用 `( )` 包裹,匹配内容会被捕获到 `$matches` 数组。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
phppreg_match('/(\d{4})-(\d{2})/', '2023-05', $matches);
|
|
|
|
|
preg_match('/(\d{4})-(\d{2})/', '2023-05', $matches);
|
|
|
|
|
// $matches[0] = "2023-05", $matches[1] = "2023", $matches[2] = "05"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- **非捕获组**:用 `(?: )` 包裹,不捕获内容(提升性能)。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
phppreg_match('/(?:\d{3})-(\d{4})/', '123-4567', $matches);
|
|
|
|
|
preg_match('/(?:\d{3})-(\d{4})/', '123-4567', $matches);
|
|
|
|
|
// $matches[1] = "4567"(仅捕获第二组)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- **反向引用**:用 `\n`(n 为组号)引用已捕获的内容。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php
|
|
|
|
|
|
|
|
|
|
preg_match('/(\w+)\s\1/', 'test test', $matches); // 匹配重复单词
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -196,11 +192,9 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- **分支匹配**:用 `|` 表示“或”关系。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php
|
|
|
|
|
|
|
|
|
|
preg_match('/(cat|dog)/', 'I have a cat'); // 匹配 "cat"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- **条件匹配**:PCRE 支持 `(?(condition)yes|no)` 语法(较少用)。
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
@ -216,6 +210,7 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
| `s` | 让 `.` 匹配换行符 | `/.*./s` 匹配跨行文本 |
|
|
|
|
|
| `u` | 支持 UTF-8 编码 | `/\p{L}/u` 匹配 Unicode 字母 |
|
|
|
|
|
| `x` | 忽略模式中的空白和注释 | `/a b /x` 匹配 `ab` |
|
|
|
|
|
| `e` | 匹配后的字符串作为php执行 | 新版本已经取消 |
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
@ -227,11 +222,9 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- 反向环视:`(?<=pattern)`(前面必须是 `pattern`),`(?<!pattern)`(前面不能是 `pattern`)。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php
|
|
|
|
|
|
|
|
|
|
preg_match('/(?<=\d)abc/', '123abc'); // 匹配 "abc"(前面是数字)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- **原子组**:`(?>pattern)` 防止回溯(性能优化)。
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
@ -241,8 +234,6 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
- 使用 `\p{L}` 匹配任意字母,`\p{N}` 匹配数字,需配合 `u` 修饰符。后一个`/`后面可以有模式修饰符`i`,`m`,`s`,`u`,`x`等。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php
|
|
|
|
|
|
|
|
|
|
preg_match('/\p{L}+/u', '中文'); // 匹配 "中文"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
@ -253,7 +244,7 @@ PHP 的正则表达式基于 **PCRE(Perl Compatible Regular Expressions)**
|
|
|
|
|
模式匹配字符串的开始和结束一般是`/`(PHP可以有其他的替代,具体请参考手册);
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
php// 匹配邮箱
|
|
|
|
|
// 匹配邮箱
|
|
|
|
|
$email = "user@example.com";
|
|
|
|
|
if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) {
|
|
|
|
|
echo "Valid email";
|
|
|
|
|
|