PHP正則表達式入門教程[轉]
$regex
= '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i' ; $matches
= array(); if (preg_match($regex,
$str, $matches)){ var_dump($matches); } echo
"\n" ; |
preg_match中的$matches[0]將包含與整個模式匹配的字符串。
使用"#"定界符的代碼如下.這個時候對"/"就不轉義!
$matches
= array(); if (preg_match($regex,
$str, $matches)){ var_dump($matches); } echo
"\n" ; |
¤ 修飾符:用於改變正則表達式的行爲。
我們看到的('/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html/i')中的最後一個"i"就是修飾符,表示忽略大小寫,還有一個我們經常用到的是"x"表示忽略空格。
貢獻代碼:
$regex =
'/HELLO/' ; $str =
'hello
word' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ echo 'No
i:Valid Successful!' , "\n" ; } if (preg_match( $regex . 'i' ,
$str ,
$matches )){ echo 'YES
i:Valid Successful!' , "\n" ; } |
¤ 字符域:[\w]用方括號擴起來的部分就是字符域。
¤ 限定符:如[\w]{3,5}或者[\w]*或者[\w]+這些[\w]後面的符號都表示限定符。現介紹具體意義。
{3,5}表示3到5個字符。{3,}超過3個字符,{,5}最多5個,{3}三個字符。
* 表示0到多個
+ 表示1到多個。
¤ 脫字符號
^:
> 放在字符域(如:[^\w])中表示否定(不包括的意思)——“反向選擇”
> 放在表達式之前,表示以當前這個字符開始。(/^n/i,表示以n開頭)。
注意,我們經常管"\"叫"跳脫字符"。用於轉義一些特殊符號,如".","/"
$regex =
'/(?<=c)d(?=e)/' ;
/*
d 前面緊跟c, d 後面緊跟e*/ $str =
'abcdefgk' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
否定意義:
$regex =
'/(?<!c)d(?!e)/' ;
/*
d 前面不緊跟c, d 後面不緊跟e*/ $str =
'abcdefgk' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
$regex =
'/HE(?=L)LO/i' ; $str =
'HELLO' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
打印不出結果!
$regex =
'/HE(?=L)LLO/i' ; $str =
'HELLO' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
能打印出結果!
說明:(?=L)意思是HE後面緊跟一個L字符。但是(?=L)本身不佔字符,要與(L)區分,(L)本身佔一個字符。
$regex =
'/^(Chuanshanjia)[\w\s!]+\1$/' ;
$str =
'Chuanshanjia
thank Chuanshanjia' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
$regex =
'/(?P<author>chuanshanjia)[\s]Is[\s](?P=author)/i' ; $str =
'author:chuanshanjia
Is chuanshanjia' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
運行結果
惰性匹配(記住:會進行兩部操作,請看下面的原理部分)
格式:限定符?
原理:"?":如果前面有限定符,會使用最小的數據。如“*”會取0個,而“+”會取1個,如過是{3,5}會取3個。
先看下面的兩個代碼:
代碼1.
<?php $regex =
'/heL*/i' ; $str =
'heLLLLLLLLLLLLLLLL' ; if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
結果1.
代碼2
<?php $regex =
'/heL*?/i' ; $str =
'heLLLLLLLLLLLLLLLL' ; if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
結果2
代碼3,使用“+”
<?php $regex =
'/heL+?/i' ; $str =
'heLLLLLLLLLLLLLLLL' ; if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
結果3
代碼4,使用{3,5}
<?php $regex =
'/heL{3,10}?/i' ; $str =
'heLLLLLLLLLLLLLLLL' ; if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
結果4
$regex =
'/ ^host=(?<!\.)([\d.]+)(?!\.)
(?#主機地址) \| ([\w!@#$%^&*()_+\-]+)
(?#用戶名) \| ([\w!@#$%^&*()_+\-]+)
(?#密碼) (?!\|)$/ix'; $str =
'host=192.168.10.221|root|123456' ; $matches =
array (); if (preg_match( $regex ,
$str ,
$matches )){ var_dump( $matches ); } echo "\n" ; |
特殊字符 | 解釋 |
* | 0到多次 |
+ | 1到多次還可以寫成{1,} |
? | 0或1次 |
. | 匹配除換行符外的所有單個的字符 |
\w | [a-zA-Z0-9_] |
\s | 空白字符(空格,換行符,回車符)[\t\n\r] |
\d | [0-9] |
原文:http://www.cnblogs.com/baochuan/archive/2012/03/12/2391135.html