php –正则表达式

在线正则检测工具链接

正则表达式语法参考
    .                       除换行符以外的所有字符
    \.                      转义字符(另有\*, \(, \\, 等等.)
    ^                       字符串开头
    $                       字符串结尾
    \d,\w,\s                一个数字, 字符 [A-Za-z0-9_], 空格.
    \D,\W,\S                一个非数字, 非字符 [A-Za-z0-9_], 非空格.
    [abc]                   a,b或c中的一个.
    [a-z]                   a到z中的一个字母.
    [^abc]                  除a,b或c的一个字符.
    aa|bb                   aa或者bb.
    ?                       0次或1次匹配.
    *                       任意次数匹配.
    +                       一次或一次以上匹配.
    {n}                     n次精确匹配.
    {n,}                    n次或n次以上匹配.
    {m,n}                   最少m次,最多n次匹配.
    ??,*?,+?,{n}?, etc.     同上,但尽可能少次数的匹配.
    (expr)                  捕获 expr 子模式,以 \1使用它, 等等.
    (?:expr)                忽略捕获的子模式.
    (?=expr)                正向预查模式 expr.
    (?!expr)                负向预查模式 expr.

php正则函数介绍与区别

preg_match & preg_match_all

//执行一个正则表达式的匹配
preg_match(string $pattern , string $subject[,array $matches])
//执行一个全局正则表达式匹配
preg_match_all(string $pattern , string $subject[,array $matches])

preg_match() 返回 $pattern的匹配次数0或1次,因为匹配到第一个后就会停止搜索,而 preg_match_all() 将会一直搜索 $subject 到结尾

<?php
    $pattern='/[0-9]/';
    $object='werdf43ofd9as2noi4';
    $m1=$m2=array();
    preg_match($pattern,$object,$m1);
    preg_match_all($pattern,$object,$m2);
    var_dump($m1);
    var_dump($m2);
//结果:
Array
(
    [0] => 4
)
Array
(
    [0] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 9
            [3] => 2
            [4] => 4
        )
)

preg_fiflter & preg_replace

//只返回经过正则匹配的结果
preg_fiflter(mixed $pattern , mixed $replacement , mixed $subject)
//放回所有结果
preg_replace(mixed $pattern , mixed $replacement , mixed $subject)
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');

echo "preg_filter returns\n";
print_r(preg_filter($pattern, $replace, $subject));

echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject));
//结果:
    preg_filter returns
    Array
    (
        [0] => A:C:1
        [1] => B:C:a
        [2] => A:2
        [3] => B:b
        [4] => A:3
        [7] => A:4
    )
    preg_replace returns
    Array
    (
        [0] => A:C:1
        [1] => B:C:a
        [2] => A:2
        [3] => B:b
        [4] => A:3
        [5] => A
        [6] => B
        [7] => A:4
    )

其他正则函数介绍

  • preg_quote(string $pattern) — 转义正则表达式字符,有时候匹配字符中包含正则表达式语法,需要进行转义才能进行匹配
    正则表达式特殊字符有: . + * ? [ ^ ] $ ( ) { } = ! < > | : -

  • preg_split(string $pattern , string $subject) — 通过一个正则表达式分隔字符串,类似于explode函数

  • preg_grep — 返回匹配模式的数组条目

$array = array("23.32","22","12.009","23.43.43");
         print_r(preg_grep("/^(\d+)?\.\d+$/",$array));
    //结果:
    Array
    (
        [0] => 23.32
        [2] => 12.009
    )
  • preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换

<?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
function next_year($matches)
{
  // 通常: $matches[0]是完成的匹配
  // $matches[1]是第一个捕获子组的匹配
  // 以此类推
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);

?>
//结果
April fools day is 04/01/2003
Last christmas was 12/24/2002
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值