简体   繁体   中英

php regex - bug with pattern

Just wondering if someone can help me understand why my regular expression is matching a particular line. Here's the input data:

         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# ) 

And here's my code:

  $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

Here are the results of the print / echo statements:

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

My question is why the first item gets matched when it doesn't start with a number? Would you mind pointing out where my bug is or what I'm not understanding?

Thanks.

将正则表达式中的([0-9]*)更改为([0-9]+)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM