標籤: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:正則匹配-量詞