-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 回答