2012-08-22 94 views
-1

只是想知道是否有人可以幫助我理解爲什麼我的正則表達式匹配一個特定的行。這裏的輸入數據:php正則表達式 - 模式錯誤

  Array ([0] => [24;1H [24;16H [24;1H [?25h [24;16H [24;16Hshow vlans [24;16H [?25h [24;26H [24;0H E [24;1H [24;26H [24;1H [2K [24;1H [?25h [24;1H [1;24r [24;1H [1] => Status and Counters - VLAN Information [2] => [3] => Maximum VLANs to support : 256 [4] => Primary VLAN : MANAGEMENT [5] => Management VLAN : [6] => [7] => VLAN ID Name | Status Voice Jumbo [8] => ------- -------------------------------- + ---------- ----- ----- [9] => 1 DEFAULT_VLAN | Port-based No No [10] => 3 MANAGEMENT | Port-based No No [11] => 8 SERVER_VLAN | Port-based No No [12] => 16 iLOSERS | Port-based No No [13] => 20 BACS_VLAN | Port-based No No [14] => 33 VLAN_33 | Port-based No No [15] => 64 ISM_VLAN | Port-based No No [16] => 65 DSLAM1 | Port-based No No [17] => 80 VOIP_VLAN | Port-based No No [18] => 96 DZONE | Port-based No No [19] => 128 BACNET_128 | Port-based No No [20] => 131 BACNET_131 | Port-based No No [21] => [22] => [23] => [1;24r [24;1H [24;1H [2K [24;1H [?25h [24;1H [24;11#) 

這裏是我的代碼:

$vlandetailsArray = array(); 
    foreach ($data as $vlandetails) { 

    $pattern = '/(\s+)([0-9]*)(\s+)([a-z_0-9]*)(\s*)(\|)(\s+)([a-z0-9_-]*)(\s*)(\w*)(\s*)(\w*)/i'; 
    if (preg_match($pattern, $vlandetails, $matches)) { 
     echo 'raw data is: '.($vlandetails).'<br>'; 
     echo 'results from print_r:'; 
     print_r($matches[2]);  
     echo '<br>VlanId is: '.$matches[2].'<br>'; 
     } //end if 
    } //end for 

這裏是打印結果/ echo語句:

raw data is: VLAN ID Name | Status Voice Jumbo 
results from print_r: 
VlanId is: 
raw data is: 1 DEFAULT_VLAN | Port-based No No 
results from print_r:1 
VlanId is: 1 
raw data is: 3 MANAGEMENT | Port-based No No 
results from print_r:3 
VlanId is: 3 
raw data is: 8 SERVER_VLAN | Port-based No No 
results from print_r:8 

我的問題是,爲什麼第一項獲得當它不是以數字開頭的時候匹配? 你介意指出我的錯誤在哪裏,或者我不瞭解什麼?

謝謝。

+4

我沒有看到你的標題如何有什麼與你的問題。即使在你的輸出中echo和print_r也顯示相同的數字。 – Cfreak

+0

是的,你是對的。我開始了兩個不同的問題,並將問題1的內容發佈到question2的窗口中。對困惑感到抱歉。謝謝你的耐心。 – dot

回答

3

更改正則表達式的([0-9]*)([0-9]+)

+0

doh!我一直在看這太久。我錯過了。 Thans。 – dot