> For the complete documentation index, see [llms.txt](https://phper.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://phper.shujuwajue.com/zheng-ze-biao-da-shi.md).

# 正则表达式

正则表达式的作用：分割、查找、匹配、替换字符串

\w 是数字字母下划线，并不是只包含字母

## 后向引用

```php
<?php
$str = '<b>abc</b>';
$pattern = '/<b>(.*)<\/b>/';
$str = preg_replace($pattern, '\\1', $str);
var_dump($str);
```

## 贪婪模式

```php
<?php
$str = '<b>abc</b><b>bcd</b>';
$pattern = '/<b>.*?<\/b>/';
//$pattern = '/<b>.*?<\/b>/U';
$strs = preg_match_all($pattern, $str, $match);
var_dump($match);
```

## 正则表达式PCRE函数：

* preg\_match
* preg\_match\_all()、
* preg\_replace()
* preg\_split()

## 中文匹配

UTF-8汉字编码范围是0x4e00-0x9fa5,在ANSI(gb2312)环境,0xb0-0xf7,0xa1-0xfe

UTF-8要使用u模式修正符使模式字符串被当成UTF-8，在ANSI(gb2312)环境下,要使用chr将Ascii码转换为字符

```php
<?php

$str = '中文';

$pattern = '/[\x{4e00}-\x{9fa5}]/u';

//$pattern = '/['.chr(0xb0).'-'.chr(0xf7).']['.chr(0xa1).'-'.chr(0xfe).']/';

preg_match($pattern, $str, $match);

var_dump($match);
```

## 示例：

```php
<?php

// 请写出以139开头的11位手机号码的正则表达式

// 13988888888
//
$str = '13888888888';

$pattern = '/^139\d{8}$/';

preg_match($pattern, $str, $match);

var_dump($match);
```

```php
<?php

// 请匹配所有img标签中的src的值

$str = '<img alt="高清无码" id="av" src="av.jpg" />';

$pattern = '/<img.*?src="(.*?)".*?\/?>/i';

preg_match($pattern, $str, $match);

var_dump($match);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://phper.shujuwajue.com/zheng-ze-biao-da-shi.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
