php正則:匹配(),{},[]小括弧,大括弧,中括弧裡面的內容

來源:互聯網
上載者:User

標籤:大括弧   data   str   返回   emerge   驚嘆號   等於   highlight   match   

php正則:匹配(),{},[]小括弧,大括弧,中括弧裡面的內容

比如有一段字元:

$s=‘60c8{"code":"200","message":"success","data":[{"_id":"780521","time":1476768196,"position":{"long":108.90415,"lat":35.25623},"speed":495.677952,"anum":"B6552","atype":"A321","atypename":"Airbus A321-231","angle":"198","vspeed":"-576","device":"9d26b827eb566225","onground":"0","updatetime":1476768198,"height":3581.4,"fnum":"CSN3722 ","alert":"0","spi":"0","squawk":"1107","emergency":"0"}]}‘;

  

我要匹配,{}括弧裡面的內容,單獨拿出來。

 preg_match(‘/(?:\{)(.*)(?:\})/i‘, $s, $match);var_dump($match);

  

 

說明:

PHP使用Regex提取字串中角括弧<>、小括弧()、中括弧[]、大括弧{}中的字元樣本,需要的朋友可以參考下

$str="你好<我>(愛)[北京]{天安門}";echo f1($str); //返回你好 echo f2($str); //返回我 echo f3($str); //返回愛 echo f4($str); //返回北京 echo f5($str); //返回天安門function f1($str) { $result = array(); preg_match_all("/^(.*)(?:<)/i",$str, $result); return $result[1][0]; } function f2($str) { $result = array(); preg_match_all("/(?:<)(.*)(?:>)/i",$str, $result); return $result[1][0]; }function f3($str) { $result = array(); preg_match_all("/(?:\()(.*)(?:\))/i",$str, $result); return $result[1][0]; }function f4($str) { $result = array(); preg_match_all("/(?:\[)(.*)(?:\])/i",$str, $result); return $result[1][0]; }function f5($str) { $result = array(); preg_match_all("/(?:\{)(.*)(?:\})/i",$str, $result); return $result[1][0]; } 

 

PS: (?:字元) 表示不捕獲這個字元。貌似PHP不支援將字元換成括弧。 
否則的話可以將環視給嵌套進去,就可以迴圈匹配了。 
PS2:環視:(?!) (?=) (?<!) (?<=) 
有小於符號的在右側匹配,沒有的在左側匹配。驚嘆號表示不等,等號表示相等。 
PS3:都過了驗證器的驗證,驗證器見參考資料。 

 

php正則:匹配(),{},[]小括弧,大括弧,中括弧裡面的內容

聯繫我們

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