PHP正则表达式入门教程[转]
??? preg_match中的$matches[0]將包含與整個模式匹配的字符串。?
??? 使用"#"定界符的代碼如下.這個時候對"/"就不轉義!
$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i'; $str = 'http://www.youku.com/show_page/id_ABCDEFG.html'; $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開頭)。
????? 注意,我們經常管"\"叫"跳脫字符"。用于轉義一些特殊符號,如".","/"
?
通配符(lookarounds):斷言某些字符串中某些字符的存在與否! lookarounds分兩種:lookaheads(正向預查 ?=)和lookbehinds(反向預查?<=)。 > 格式: 正向預查:(?=) 相對應的 (?!)表示否定意思 反向預查:(?<=) 相對應的 (?<!)表示否定意思 前后緊跟字符 $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)本身占一個字符。
捕獲數據 沒有指明類型而進行的分組,將會被獲取,供以后使用。 > 指明類型指的是通配符。所以只有圓括號起始位置沒有問號的才能被捕捉。 > 在同一個表達式內的引用叫做反向引用。 > 調用格式: \編號(如\1)。 $regex = '/^(Chuanshanjia)[\w\s!]+\1$/'; $str = 'Chuanshanjia thank Chuanshanjia'; $matches = array();if(preg_match($regex, $str, $matches)){var_dump($matches); }echo "\n";?
> 避免捕獲數據 格式:(?:pattern) 優點:將使有效反向引用數量保持在最小,代碼更加、清楚。 >命名捕獲組 格式:(?P<組名>) 調用方式 (?P=組名) $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
正則表達式的注釋 格式:(?# 注釋內容) 用途:主要用于復雜的注釋 貢獻代碼:是一個用于連接MYSQL數據庫的正則表達式 $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
?
總結
以上是生活随笔為你收集整理的PHP正则表达式入门教程[转]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十进制与二进制间的相互转换
- 下一篇: Javascript Prototype