2

I'm absolutely terrible at regex; can anyone help me solve the expression I need in order to separate two values I need from a log file?

Log file example.

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}

With my current code I am able to separate the IP address, however I now need both the state (added/removed) and the IP address. Here is my current code.

preg_match_all("/.*{(.*)}.*/", $a, $b);

What do I need to replace "/.{(.)}.*/" with in order to grab both the state and the IP address to store into the array?

4

3 に答える 3

3

これに一致する正規表現は実際には必要ありません。preg_split()とを区切り文字として使用して空白で分割し、 のような単純な関数を使用して IP アドレスから\s+中かっこを取り除くことができます。{}trim()

$output = array();

// While reading line by line...
$parts = preg_split('/\s+/', $line);
$output[] = array(
  'state' => $parts[3],
  'ip' => trim($parts[4], '{}')
);

http://codepad.viper-7.com/fD8kgQ

于 2013-01-29T03:04:58.310 に答える
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 ...
}

外;

配列
(
    [0] => 配列
        (
            [0] => 2013 年 1 月 28 日 8:43:22 PM 削除 {178.76.234.41}
            [時間] => 2013 年 1 月 28 日午後 8 時 43 分 22 秒
            [1] => 2013 年 1 月 28 日午後 8 時 43 分 22 秒
            [状態] => 削除
            [2] => 削除されました
            [IP] => 178.76.234.41
            [3] => 178.76.234.41
        )

    [1] => 配列
        (
            [0] => 2013 年 1 月 28 日 8:43:22 PM 削除 {78.105.26.0}
            [時間] => 2013 年 1 月 28 日午後 8 時 43 分 22 秒
            [1] => 2013 年 1 月 28 日午後 8 時 43 分 22 秒
            [状態] => 削除
            [2] => 削除されました
            [IP] => 78.105.26.0
            [3] => 78.105.26.0
        )
    ...
于 2013-01-29T03:32:00.180 に答える
1

この 2 つの単語だけを含める必要がある場合は、次のようなことを試しましたか?

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 ) )
于 2013-01-29T03:04:19.197 に答える