PHPRegex基礎入門_Regex

來源:互聯網
上載者:User

思維導圖

介紹

Regex,大家在開發中應該是經常用到,現在很多開發語言都有Regex的應用,比如JavaScript、Java、.Net、PHP 等,我今天就把我對Regex的理解跟大家嘮嘮,不當之處,請多多指教!

需要知道的術語——下面的術語你知道多少?

Δ  定界符
Δ  字元域
Δ  修飾符
Δ  限定符
Δ  脫字元
Δ  萬用字元(正向預查,反向預查)
Δ  反向引用
Δ  惰性匹配
Δ  注釋
Δ  零字元寬

定位

我們什麼時候使用Regex呢?不是所有的字元操作都用正則就好了,php在某些方面用正則反而影響效率。當我們遇到複雜文本資料的解析時候,用正則是比較好的選擇。

優點

Regex在處理複雜字元操作的時候,可以提高工作效率,也在一定程度節省你的代碼量。

缺點

我們在使用Regex的時候,複雜的Regex會加大代碼的複雜度,讓人很難理解。所以我們有的時候需要在Regex內部添加註釋。

通用模式

¤ 定界符,通常使用 “/”做為定界符開始和結束,也可以使用”#”。
什麼時候使用”#”呢?一般是在你的字串中有很多”/”字元的時候,因為正則的時候這種字元需要轉義,比如uri。
使用”/”定界符的代碼如下.

$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";

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";

¤ 修飾符:用於改變Regex的行為。

我們看到的(‘/^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

Regex的注釋

格式:(?# 注釋內容)
用途:主要用於複雜的注釋

貢獻代碼:是一個用於串連MYSQL資料庫的Regex

$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";

特殊字元

想要學好phpRegex,僅僅學習這一篇文章是遠遠夠的,希望大家堅持學習,閱讀phpRegex相關文章。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.