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
        )

)

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~