我在正则表达式上非常糟糕;任何人都可以帮我解决我需要的表达式,以便从日志文件中分离出我需要的两个值吗?
日志文件示例。
1/28/2013 8:43:22 PM Removed {178.76.234.41}
1/28/2013 8:43:22 PM Removed {78.105.26.0}
1/28/2013 8:43:22 PM Removed {24.165.198.12}
1/28/2013 8:43:23 PM Added {178.76.234.41}
1/28/2013 8:43:23 PM Added {69.246.227.43}
使用我当前的代码,我能够分离IP地址,但是我现在需要状态(添加/删除)和IP地址。这是我目前的代码。
preg_match_all("/.*{(.*)}.*/", $a, $b);
我需要更换什么" /。 {(。)}。* /"用以获取要存储到数组中的状态和IP地址?
答案 0 :(得分:3)
您实际上并不需要正则表达式来匹配它。您可以使用preg_split()
和\s+
作为分隔符在空白处拆分,然后使用{}
之类的简单函数从IP地址中删除大括号trim()
。
$output = array();
// While reading line by line...
$parts = preg_split('/\s+/', $line);
$output[] = array(
'state' => $parts[3],
'ip' => trim($parts[4], '{}')
);
答案 1 :(得分:1)
如果这些是您需要包含的两个单词,您是否尝试过类似的内容?
preg_match_all("~(Removed|Added)\s+{(.*)}~i", $a, $b);
所以总的来说:
$a = '1/28/2013 8:43:22 PM Removed {178.76.234.41}
1/28/2013 8:43:22 PM Removed {78.105.26.0}
1/28/2013 8:43:22 PM Removed {24.165.198.12}
1/28/2013 8:43:23 PM Added {178.76.234.41}
1/28/2013 8:43:23 PM Added {69.246.227.43}';
preg_match_all("~(Removed|Added)\s+{(.*)}~i", $a, $b);
print_r($b);
导致这个:
Array ( [0] => Array ( [0] => Removed {178.76.234.41} [1] => Removed {78.105.26.0} [2] => Removed {24.165.198.12} [3] => Added {178.76.234.41} [4] => Added {69.246.227.43} ) [1] => Array ( [0] => Removed [1] => Removed [2] => Removed [3] => Added [4] => Added ) [2] => Array ( [0] => 178.76.234.41 [1] => 78.105.26.0 [2] => 24.165.198.12 [3] => 178.76.234.41 [4] => 69.246.227.43 ) )
答案 2 :(得分:1)
我认为这适合你;
$s = '1/28/2013 8:43:22 PM Removed {178.76.234.41}
1/28/2013 8:43:22 PM Removed {78.105.26.0}
1/28/2013 8:43:22 PM Removed {24.165.198.12}
1/28/2013 8:43:23 PM Added {178.76.234.41}
1/28/2013 8:43:23 PM Added {69.246.227.43}';
preg_match_all('~(?P<TIME>.*PM)\s+(?P<STATE>Added|Removed)\s+{(?P<IP>.*)}~i', $s, $m, PREG_SET_ORDER);
print_r($m);
// or
foreach ($m as $log) {
printf("Time: %s, State: %s, Ip: %s\n", $log['TIME'], $log['STATE'], $log['IP']);
// Time: 1/28/2013 8:43:22 PM, State: Removed, Ip: 178.76.234.41 ...
}
出;
Array ( [0] => Array ( [0] => 1/28/2013 8:43:22 PM Removed {178.76.234.41} [TIME] => 1/28/2013 8:43:22 PM [1] => 1/28/2013 8:43:22 PM [STATE] => Removed [2] => Removed [IP] => 178.76.234.41 [3] => 178.76.234.41 ) [1] => Array ( [0] => 1/28/2013 8:43:22 PM Removed {78.105.26.0} [TIME] => 1/28/2013 8:43:22 PM [1] => 1/28/2013 8:43:22 PM [STATE] => Removed [2] => Removed [IP] => 78.105.26.0 [3] => 78.105.26.0 ) ...