2012-08-10 55 views
3

我有以下代碼:新手PHP正則表達式問題

<?php 
     $data="000ffe-fcc9f4 1 000fbe-fccabe"; 
     $pattern='/([0-9A-F]{6})-([0-9A-F]{6})$/i'; 
     echo "the pattern we are using is: ".$pattern."<BR>"; 

     preg_match_all($pattern,$data,$matches, PREG_SET_ORDER); 

     print_r($matches[0]); 

>

我不明白爲什麼它沒有找到這兩個MAC地址作爲匹配?

下面是頁面上的輸出看起來像:

the pattern we are using is: /([0-9A-F]{6})-([0-9A-F]{6})$/i 
Array ([0] => 000fbe-fccabe [1] => 000fbe [2] => fccabe) 

我期待這個元素[0]將包含兩個000ffe-fcc9f4和000fbe-fccabe。 你能告訴我我做錯了什麼嗎?

謝謝。

回答

3

找不到它們的原因是因爲在正則表達式的末尾有一個$,這意味着它只會匹配字符串末尾的那個模式。

嘗試更改$pattern/([0-9A-F]{6})-([0-9A-F]{6})/i並且應該匹配兩者。

+0

doh!非常感謝你。修復它 – dot 2012-08-10 00:56:01

+1

http://ideone.com/GSp1W – 2012-08-10 01:00:51