php pregRegex

來源:互聯網
上載者:User

標籤:style   http   io   for   ar   cti   line   new   sp   


Note:

反斜線在單引號字串和雙引號字串 中都有特殊含義,因此要匹配一個反斜線, 模式中必須寫為 ”\\\\”。 譯註: “/\\/”, 首先它作為字串,反斜線會進行轉義, 那麼轉義後的結果是/\/,這個才是Regex引擎拿到的模式, 而Regex引擎也認為 \ 是轉義標記,它會將分隔字元 / 進行轉義, 從而得到的是一個錯誤,因此,需要 4 個反斜線才可以匹配一個反斜線。

事實上

 

$attribute = "sdfsdf.555";preg_match(‘/([\w]+)(\.)([0-5]|.)/‘, $attribute, $matches);Array(    [0] => sdfsdf.555    //匹配([\w]+)(\.)([0-5]|.)  如果整個模式比對失敗則不會繼續匹配.例如$attribute = "sssss555"    [1] => sdfsdf    //匹配(\w+)    [2] => .    //匹配(\.)    [3] => 555    //匹配([0-5]*))

 preg_replace_callback

 將字元竄中Regex匹配到的每一個值用回呼函數處理並返回..

函數會在第一次匹配後 繼續搜尋.

首先會匹配得到[{summary}]

接著會繼續搜尋匹配得到[{items}]

最後匹配得到[{form}]

下個例子..Regex..匹配的結果分別是..[{summary}],[{items}]  [{form}]

 將三個值用於回呼函數...

 

比較下面兩個匹配值的不同:

$layout = "{summary}\n{items}\n{form}";$content = preg_replace_callback("/{\\w+}/", function ($matches) {print_r($matches);  return $matches[0];}, $layout);print_r($content);$layout = "{summary}-{items}-{form}";$content = preg_replace_callback("/({\\w+})(-)/", function ($matches) {print_r($matches);//["{summary}-","{summary}","-"]return $matches[0];}, $layout);print_r($content);

 

php pregRegex

聯繫我們

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