ChatGPT体验 AI绘画体验
php用正则表达式匹配所有5位数
提问人:ppw_crxis 时间:2023-04-19 公开状态:

提问人:ppw_crxis 时间:2023-04-19 公开状态:


可以使用 \b\d{5}\b 的正则表达式来匹配所有5位数,其中:
- \d 表示匹配任意数字字符。
- {5} 表示匹配前面的字符或字符集合恰好出现了5次。
- \b 表示匹配单词边界。
示例代码:
```
$pattern = '/\b\d{5}\b/';
$text = '123 45678 90123 456 7890';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);
```
输出结果:
```
Array
(
[0] => 45678
[1] => 90123
[2] => 07890
)
```
注意,正则表达式默认是贪婪匹配,即尽可能多地匹配字符。如果要匹配非贪婪模式下的最短匹配5位数,可以使用 \b\d{5}?\b 的正则表达式。