3.3. 数字
1、匹配数字
元字符
\d
和[0-9]
含义相同。
<?php
//字符串
$str = '1a2b3c';
//正则表达式
$regular = '/\d/';
//执行匹配正则表达式
preg_match_all($regular, $str, $matches);
//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';
输出结果
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
2、匹配非数字
元字符
\D
和[^0-9]
含义相同。
<?php
//字符串
$str = '1a2b3c';
//正则表达式
$regular = '/\D/';
//执行匹配正则表达式
preg_match_all($regular, $str, $matches);
//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';
输出结果
Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
)
)
推荐文章: