php基础30:正则匹配-量词

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php基础30:正则匹配-量词相关的知识,希望对你有一定的参考价值。

<?php

    //正则表达式
    
    //1.第一个正则表达式
    if("a"=="a"){
        echo "equal";
    }else{
        echo "noequal";
    }
    echo "<hr>";

    //第一个参数表示匹配模式,第二个参数匹配的字符串
    echo (preg_match(‘/php/‘,"php"));
    echo (preg_match(‘/php/‘,"php222"));
    echo (preg_match(‘/php/‘,"p2h2p2"));

    $modle = "/php/";
    $string = "php";
    if (preg_match($modle, $string)) {
        echo "success";
    }else{
        echo "fail";
    };
    echo "<hr>";

    //什么叫做匹配?:按照模式来匹配
    //匹配与相等是不同的概念

    //2.量词+:匹配任何至少包括一个前导字符串(1个或者多个)
    // 前导:前面一个字符,+的前导是h
    // h+ 的意思是只要要包括1个h
    $model = "/ph+p/";
    $string = "phhhhhhhp";
    echo(preg_match($model, $string));//1

    $model = "/ph+p/";
    $string = "phhhgggggggggp";
    echo(preg_match($model, $string));//0

    $model = "/ph+p/";
    $string = "pp";
    echo(preg_match($model, $string))."<hr>";//0


    // 3.量词*:匹配任包括0个或者多个前导字符串(0个或者多个)
    // * 虽然可以是0个,但是前导字符不能更改,更就不匹配了
    $model = "/ph*p/";
    $string = "php";
    echo(preg_match($model, $string));//1

    $model = "/ph*p/";
    $string = "pp";
    echo(preg_match($model, $string));//1

    $model = "/ph*p/";
    $string = "pbbbbp";
    echo(preg_match($model, $string));//0
    echo "<hr>";

    // 3.量词?:匹配任何包含1个或者0个前导字符
    $model = "/ph?p/";
    $string = "pp";
    echo(preg_match($model, $string));//1

    $model = "/ph?p/";
    $string = "php";
    echo(preg_match($model, $string));//1

    $model = "/ph?p/";
    $string = "phhhp";
    echo(preg_match($model, $string))."<hr>";//0

    //4.量词(.)匹配任意一个字符
    //n个点匹配n个任意字符
    $model = "/p.p/";
    $string = "php";
    echo(preg_match($model, $string));//1

    $model = "/p..p/";
    $string = "phrp";
    echo(preg_match($model, $string));//1

    $model = "/p..p/";
    $string = "php";
    echo(preg_match($model, $string));//0

    $model = "/p..p/";
    $string = "phhhp";
    echo(preg_match($model, $string))."<hr>";//0

    //5.(.与*)的配合
    //(.*)表示任意前导字符,0个或者多个
    $model = "/p.*p/";
    $string = "php";
    echo(preg_match($model, $string));//1

    $model = "/p.*p/";
    $string = "phhhhhhp";
    echo(preg_match($model, $string));//1

    $model = "/p.*p/";
    $string = "pp";
    echo(preg_match($model, $string))."<hr>";//1

    // 6.量词{x}:表示前导必须是3个
    $model = "/ph{3}p/"; // h{3}匹配3个h
    $string = "phhhp";
    echo(preg_match($model, $string));//1

    $model = "/ph{3}p/";
    $string = "php";
    echo(preg_match($model, $string))."<hr>";//0

    //7.量词{x,y}:匹配x-y个前导字符串
    $model = "/ph{3,5}p/";
    $string = "phhhp";
    echo(preg_match($model, $string));//1

    $model = "/ph{3,5}p/";
    $string = "phhhhp";
    echo(preg_match($model, $string));//1

    $model = "/ph{3,5}p/";
    $string = "phhhhhp";
    echo(preg_match($model, $string))."<hr>";//1

    //8.量词{x,}:匹配至少x个前导字符串
    $model = "/ph{3,}p/";
    $string = "phhhp";
    echo(preg_match($model, $string));//1

    $model = "/ph{3,}p/";
    $string = "phhp";
    echo(preg_match($model, $string));//0

    $model = "/ph{3,}p/";
    $string = "phhhhp";
    echo(preg_match($model, $string))."<hr>";//1

    //9.$:匹配字符串的行尾
    //$一般加载字符串的尾巴上,表示从尾巴开始匹配(以xxx结尾)
    $model = "/php/";
    $string = "cccccccccccccccc php ccccccccccc";
    echo(preg_match($model, $string));//1

    $model = "/php$/";
    $string = "cccccccccccccccc php ccccccccccc";
    echo(preg_match($model, $string));//0

    $model = "/php$/";
    $string = "cccccccccccccccccccccccccccphp";
    echo(preg_match($model, $string))."<hr>";//1

    //10. ^ 表示从头开始匹配
    // 表示 以 xxx 开始
    $model = "/^php/";
    $string = "cccccccccccccccccccccccccccphp";
    echo(preg_match($model, $string));//0

    $model = "/^php/";
    $string = "phpcccccccccccccccccccccccccccphp";
    echo(preg_match($model, $string));//1

    //如果^和$一起使用,表示匹配一模一样的,那就使用==即可
    $model = "/^php$/";
    $string = "phpcccccccccccccccccccccccccccphp";
    echo(preg_match($model, $string))."<hr>";//1

    // 10. | : 匹配字符串的左边或者右边
    //条件选择符号
    $model = "/php|jsp/";
    $string = "php";
    echo(preg_match($model, $string));//1

    $model = "/php|jsp/";
    $string = "jsp";
    echo(preg_match($model, $string));//0

    $model = "/php|jsp/";
    $string = "asp";
    echo(preg_match($model, $string));//0

    $model = "/php|jsp|asp/";
    $string = "asp";
    echo(preg_match($model, $string))."<hr>";//1

    // 11. () : 包围一个字符分组或者
    //讲到后面才可以理解

?>

 

以上是关于php基础30:正则匹配-量词的主要内容,如果未能解决你的问题,请参考以下文章

Javascript正则表达式量词:匹配零次或多次是啥意思

6.正则表达式

phplinuxjavascript 正则表达式

bash 中的正则表达式量词——简单与扩展匹配 n 次

正则表达式之量词

正则进阶之,回溯, (贪婪* 非贪婪+? 独占++)三种匹配量词