Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析端口数据的php正则表达式_Php_Regex - Fatal编程技术网

解析端口数据的php正则表达式

解析端口数据的php正则表达式,php,regex,Php,Regex,我有以下字符串: $teststring = " Flow Link Back Mdix 端口类型双工速度负控制状态压力模式 fa1 100米铜------下降-- fa2 100米铜缆------下降-- fa3 100米铜缆------下降-- fa4 100米铜缆--------下降-- fa5 100米铜缆------下降-- fa6 100米铜缆------下降-- fa7

我有以下字符串:

$teststring = " 

                                         Flow Link          Back   Mdix
端口类型双工速度负控制状态压力模式


fa1 100米铜------下降--
fa2 100米铜缆------下降--
fa3 100米铜缆------下降--
fa4 100米铜缆--------下降--
fa5 100米铜缆------下降--
fa6 100米铜缆------下降--
fa7 100米铜线--------------
fa8 100米铜缆------下降--
gi1 1G-Combo-C------向下--
gi2 1G-Combo-C满100启用关闭向上禁用关闭

                                      Flow    Link        
Ch型双工速度负控制状态


Po1-----不存在 Po2--不存在 Po3-----不存在 Po4-----不存在 Po5-----不存在 Po6-----不存在 Po7-----不存在 Po8--“不存在” )

我试图解析每个字段。 以下是我目前掌握的代码:

$teststring = explode("<BR>", $teststring);     
$vlandetailsArray = array();
foreach ($teststring as $vlandetails) 
{            
  //              port     space    type     space   duplex          speed       space  neg         
  $pattern = '/([a-z0-9]*)(\s*)([a-z0-9\-]*)(\s*)[(Full)|(\-{2})](\s*)[(\-)+|(100)](\s*)[(--)*|(Enabled)](\s*)[(--)*|(Off)]/i';

  if (preg_match($pattern, $vlandetails, $matches)) 
  {  
       echo 'match 0: '.$matches[0].'<br>';  //0 index always returns all matches
      }
我不明白为什么它不接下这样的线:

gi2      1G-Combo-C   Full    100   Enabled  Off  Up          Disabled Off
你能告诉我我错过了什么/做错了什么吗?
仅供参考。我仍然在玩我的正则表达式,所以你会注意到有时我使用模式({2}),有时使用-+等等

编辑1

我已经修改了teststring

        $this->_data  = str_replace(chr(10),"<BR>",$this->_data ,$count);//strip New line
        $this->_data  = str_replace(chr(13),'',$this->_data ,$count);//strip carriage return    
其中$this->_数据包含原始数据。我打开此文件并复制了所有内容…然后粘贴到我的teststring变量中。
话虽如此,我已经在一个文本编辑器中分析了这个文件,我可以看到原始字符串和修改后的字符串之间唯一不同的地方是它已经被删除了所有的CRLF。但是如果有帮助,我已经删除了这个逻辑。 我还包括一个文本编辑器中未修改数据的屏幕截图。 谢谢 试试这个:

#[a-z]{2}\d\s[^\s]+ [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si
必须启用不区分大小写模式

另一个,用来捕获…介面名称

#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si
完整代码示例:

<?php

$teststring = " 
Flow Link Back Mdix 
Port Type Duplex Speed Neg ctrl State Pressure Mode 
-------- ------------ ------ ----- -------- ---- ----------- -------- ------- 
fa1 100M-Copper -- -- -- -- Down -- -- 
fa2 100M-Copper -- -- -- -- Down -- -- 
fa3 100M-Copper -- -- -- -- Down -- -- 
fa4 100M-Copper -- -- -- -- Down -- -- 
fa5 100M-Copper -- -- -- -- Down -- -- 
fa6 100M-Copper -- -- -- -- Down -- -- 
fa7 100M-Copper -- -- -- -- Down -- -- 
fa8 100M-Copper -- -- -- -- Down -- -- 
gi1 1G-Combo-C -- -- -- -- Down -- -- 
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off 

Flow Link 
Ch Type Duplex Speed Neg control State 
-------- ------- ------ ----- -------- ------- ----------- 
Po1 -- -- -- -- -- Not Present 
Po2 -- -- -- -- -- Not Present 
Po3 -- -- -- -- -- Not Present 
Po4 -- -- -- -- -- Not Present 
Po5 -- -- -- -- -- Not Present 
Po6 -- -- -- -- -- Not Present 
Po7 -- -- -- -- -- Not Present 
Po8 -- -- -- -- -- Not Present";

$pattern = '#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si';

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

foreach ($matches as $match) {
    echo '<pre>';
    var_dump($match);
    echo '</pre>';
}

/* EOF */

以下是有效的正则表达式模式:


$pattern='/([a-z]{2}\d)(\s*)([0-9a-z-])(\s)([a-z-])(\s)([0-9-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])(\s)([a-z-])/i';

这看起来确实只允许使用空格作为分隔符。是这些空格还是
\t
-s?您可以使用简单的
str\u逐行分割,然后用空格代替正则表达式。只需找出它们分隔数据的方式。@Ranty\s也应该选择制表符,除非有特定于php Re的内容gEx的我不知道。在那里我用一个完整的代码示例更新了答案。它与你的问题文本一致。我避免了“爆炸(”
)“”的内容,因为原始字符串中没有任何html换行符。迭戈,我已更新了我的帖子。你关于忽略
的评论触发了我的记忆,我正在/正在用
替换CRLF。这篇帖子中的CRLF看起来不太干净,因为其中嵌入了CRLF。无论如何,如果清理完字符串后,你可以使用sa获得它me format作为您在preg_match_all行中的第一个示例(顺便说一句,上次编辑后它现在看起来已损坏),该示例应按预期工作。如果它工作正常,请不要忘记接受答案!。
#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si
<?php

$teststring = " 
Flow Link Back Mdix 
Port Type Duplex Speed Neg ctrl State Pressure Mode 
-------- ------------ ------ ----- -------- ---- ----------- -------- ------- 
fa1 100M-Copper -- -- -- -- Down -- -- 
fa2 100M-Copper -- -- -- -- Down -- -- 
fa3 100M-Copper -- -- -- -- Down -- -- 
fa4 100M-Copper -- -- -- -- Down -- -- 
fa5 100M-Copper -- -- -- -- Down -- -- 
fa6 100M-Copper -- -- -- -- Down -- -- 
fa7 100M-Copper -- -- -- -- Down -- -- 
fa8 100M-Copper -- -- -- -- Down -- -- 
gi1 1G-Combo-C -- -- -- -- Down -- -- 
gi2 1G-Combo-C Full 100 Enabled Off Up Disabled Off 

Flow Link 
Ch Type Duplex Speed Neg control State 
-------- ------- ------ ----- -------- ------- ----------- 
Po1 -- -- -- -- -- Not Present 
Po2 -- -- -- -- -- Not Present 
Po3 -- -- -- -- -- Not Present 
Po4 -- -- -- -- -- Not Present 
Po5 -- -- -- -- -- Not Present 
Po6 -- -- -- -- -- Not Present 
Po7 -- -- -- -- -- Not Present 
Po8 -- -- -- -- -- Not Present";

$pattern = '#[a-z]{2}\d ([^\s]+) [a-z\-]+[^\s]+ ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)#si';

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

foreach ($matches as $match) {
    echo '<pre>';
    var_dump($match);
    echo '</pre>';
}

/* EOF */