php基礎30:正則匹配-量詞

來源:互聯網
上載者:User

標籤:mod   mat   jsp   正則表達   字元   cccccc   equal   pcc   正則   

<?php    //Regex        //1.第一個Regex    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:正則匹配-量詞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.