2009-06-24 78 views
0

我需要帮助找出一些正则表达式。我正在运行dig命令,我需要使用它的输出。我需要解析它,并使用PHP将它整齐排列成一个数组。正则表达式/解析挖掘输出/从双引号中提取文本

挖输出是这样的:

m0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183342" "Some text here1" 
m0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183341" "Some text here2" 

我想要得到这样的:

Array 
(
    [0] => Array 
     (
      [0] => .tkw 
      [1] => 1 
      [2] => 20090624-183342 
      [3] => Some text here1 
     ) 
    [1] => Array 
... 
) 

我只需要双引号中的内容。我可以逐行解析挖掘输出,但我认为它会更快,如果我只是运行匹配的所有正则表达式模式...

想法?

回答

0

此靠拢与单行

preg_match_all('/"([^"]+)"\s*"([^"]+)"\s*"([^"]+)"\s*"([^"]+)"/', $text, $matches, PREG_SET_ORDER); 

print_r($matches); 

然而,监守的的preg_match *功能是如何工作的完全匹配包括在每个比赛组的索引0处。如果你真的想,你可以解决这个问题。

array_walk($matches, create_function('&$array', 'array_shift($array);return $array;')); 
+0

谢谢彼得 - 你的解决方案摆脱了双引号。这是我需要的! – Steve 2009-06-25 04:33:16

2

我不知道关于PHP正则表达式,但在Perl的RE将是简单:

my $c = 0; 
print <<EOF; 
Array 
(
EOF 
foreach (<STDIN>) { 
    if (/[^"]*"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"/) { 
     print <<EOF; 
    [$c] => Array 
     (
      [0] = $1 
      [1] = $2 
      [2] = $3 
      [3] = $4 
     ) 
EOF 
     $c++; 
    } 
} 

print <<EOF; 
) 
EOF 

这有一定的局限性,即:

  • 它不如果文本工作在引号中可以省略引号(例如\"
  • 它被硬编码为仅支持四个引用值。
0

代码:

<?php 
    $str = 'm0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183342" "Some text here1" 
m0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183341" "Some text here2"'; 

    header('Content-Type: text/plain'); 
    $matches = array(); 
    preg_match_all('/(".*").*(".*").*(".*").*(".*")/U', $str, $matches, PREG_SET_ORDER); 
    print_r($matches); 
?> 

输出:

 
Array 
(
    [0] => Array 
     (
      [0] => ".tkw" "1" "20090624-183342" "Some text here1" 
      [1] => ".tkw" 
      [2] => "1" 
      [3] => "20090624-183342" 
      [4] => "Some text here1" 
     ) 

    [1] => Array 
     (
      [0] => ".tkw" "1" "20090624-183341" "Some text here2" 
      [1] => ".tkw" 
      [2] => "1" 
      [3] => "20090624-183341" 
      [4] => "Some text here2" 
     ) 

) 
+0

嗨,John!我根据你的和彼得的最终解决方案(下图)。两者的结合为我解决。欣赏你的快速周转和对细节的关注! – Steve 2009-06-25 04:34:13

0

你完全没有问什么,但它的工作,可用于与任意数量的报价串,并具有比一般的正则表达式更具有可读性的利益(在这样的代价更代码)

class GetQuotedText { 
    const STATE_OUTSIDE = 'STATE_OUTSIDE'; 
    const STATE_INSIDE = 'STATE_INSIDE'; 

    static private $input; 
    static private $counter; 
    static private $state; 
    static private $results; 

    static private $current; 
    static private $full; 
    static private $all; 

    static private function setInput($string) { 
     $this->input = $string; 

    } 

    static private function init($string) { 
     self::$current = array(); 
     self::$full   = array();  
     self::$input = $string; 
     self::$state = self::STATE_OUTSIDE; 
    } 


    static public function getStrings($string) { 
     self::init($string); 
     for(self::$counter=0;self::$counter<strlen(self::$input);self::$counter++){ 
      self::parse(self::$input[self::$counter]); 
     } 
     self::saveLine(); 
     return self::$all; 
    } 

    static private function parse($char) { 
     switch($char){ 
      case '"': 
       self::encounteredToken($char); 
       break;  
      case "\n": //deliberate fall through for "\n" and "\r" 
      case "\r": 
       self::encounteredToken($char); 
       break; 
      default: 
       if(self::$state == self::STATE_INSIDE) { 
        self::action($char); 
       } 
     } 
    } 

    static private function encounteredToken($token) { 
     switch($token) { 
      case '"': 
       self::swapState(); 
       break; 
      case "\n": //deliberate fall through for "\n" and "\r" 
      case "\r": 
       self::saveArray(); 
       self::saveLine(); 
       break; 
     } 
     return; 
    } 

    static private function swapState() { 
     if(self::$state == self::STATE_OUTSIDE) { 
      self::$state = self::STATE_INSIDE; 
     } 
     else { 
      self::$state = self::STATE_OUTSIDE;    
      self::saveArray(); 
     }    
    } 
    static public function saveLine() { 
     self::$all[] = self::$full; 
     self::$full = array(); 
     //reset state when line ends 
     self::$state = self::STATE_OUTSIDE; 
    } 

    static private function saveArray() { 
     if(count(self::$current) > 0) { 
      self::$full[] = implode ('',self::$current); 
      self::$current = array(); 
     } 
    } 

    static private function action($char) { 
     self::$current[] = $char; 
    } 
} 

$input = 'm0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183342" "Some text here1"' . "\n" . 
     'm0.ttw.mydomain.tel. 60  IN  TXT  ".tkw" "1" "20090624-183341" "Some text here2"'; 
$strings = GetQuotedText::getStrings($input); 
print_r($strings);