8.4. 否定环视
种类 | 说明 |
---|---|
(?=) | 向前查看 |
(?!) | 否定向前查看 |
(?<=) | 向后查看 |
(?<!) | 否定向后查看 |
1、匹配 $
符号后面的数字(否定向前查看)
<?php
//字符串
$str = "$30 40$ $50 60$";
//正则表达式
$regular = '/\d+(?!\$)\b/';
//执行匹配正则表达式
preg_match_all($regular, $str, $matches);
//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';
输出结果
Array
(
[0] => Array
(
[0] => 30
[1] => 50
)
)
2、匹配 $
符号前面的数字(否定向后查看)
<?php
//字符串
$str = "$30 40$ $50 60$";
//正则表达式
$regular = '/\b(?<!\$)\d+/';
//执行匹配正则表达式
preg_match_all($regular, $str, $matches);
//打印结果
echo '<pre>';
print_r($matches);
echo '</pre>';
输出结果
Array
(
[0] => Array
(
[0] => 40
[1] => 60
)
)
推荐文章: