-1

誰かが私の正規表現が特定の行に一致する理由を理解するのを手伝ってくれるかどうか疑問に思っています。入力データは次のとおりです。

         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

print/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

1 に答える 1

3

([0-9]*)正規表現のをに変更します([0-9]+)

于 2012-08-22T15:08:43.590 に答える