php preg匹配两个字符串

时间:2016-01-20 14:35:30

标签: php regex

如何使用preg match

从下面的字符串中获取IpAddr值
{{Id=0|IpAddr=172.28.190.48|gateway=172.17.00.01|mask=255.255.255.240|pref=1}|{Id=0|IpAddr=172.30.228.64|gateway=172.17.01.02|mask=255.255.255.192|pref=1}|{Id=0|IpAddr=0.0.0.0|gateway=172.19.00.01|mask=0.0.0.0|pref=1}}

我在下面试过,但没有给出结果

preg_match_all("/.*IpAddr=(.*)\|/", $string, $result_array);

1 个答案:

答案 0 :(得分:2)

你可能很多并且只得到1个结果,你需要懒惰/不匹配:

preg_match_all("/\bIpAddr=(.*?)\|/", $string, $result_array);
                             ^ Lazy / ungreedy match, take as few as possible
                 ^^ a word boundary before the IpAddr string