0

我的主题看起来像这样:

[9] 20:30:40 [SUCCESS] s-a24:1337
2
8.87
4038047
35320
4002727
[10] 20:30:40 [SUCCESS] s-d28:1337
2
2.64
4038047
37363
4000684
[11] 20:30:40 [SUCCESS] s-b29:1337
2
0.56
4038047
33066
4004981
[66] 20:30:42 [FAILURE] s-b25:1337 Timed out, Killed by signal 9
2
1.16
4038047
35274
[67] 20:30:42 [FAILURE] s-b30:1337 Timed out, Killed by signal 9

我需要创建一个匹配的多维数组

  • 成功/失败
  • 描述符(s-a24、s-d28 等)
  • 后面的 5 行数字(可以是任意长度)

可能有 0 到 5 行(5 行是成功的)

我完全不擅长正则表达式

我开始的是这个,它不起作用。

preg_match_all("/\[[0-9]+\](.*?)\[[0-9]+\]/",$output,$array);
4

5 回答 5

2

我不确定一个简单的正则表达式是否适合这里,特别是考虑到每条记录的可变数据计数。

那么,如何拆分字符串呢?

通过在每个换行符后跟一个“[”拆分它并在每个换行符处拆分每个结果,您可以使用简单的正则表达式评估结果的每一行,并轻松添加数字数据(如果有)。

问候斯特凡

于 2012-04-20T01:05:00.320 回答
0

这对我有用:

<?php

$str = '[9] 20:30:40 [SUCCESS] s-a24:1337
2
8.87
4038047
35320
4002727
[10] 20:30:40 [SUCCESS] s-d28:1337
2
2.64
4038047
37363
4000684
[11] 20:30:40 [SUCCESS] s-b29:1337
2
0.56
4038047
33066
4004981
[66] 20:30:42 [FAILURE] s-b25:1337 Timed out, Killed by signal 9
2
1.16
4038047
35274
[67] 20:30:42 [FAILURE] s-b30:1337 Timed out, Killed by signal 9';

preg_match_all("/^\[.*?\[(SUCCESS|FAILURE)\]\s(s\-\w\d{2}):1337.*?\n((.*\n){5})/m", $str, $matches, PREG_SET_ORDER);
//print_r($matches);
foreach ($matches as $match)
{
    echo $match[1] . "<br>\n";
    echo $match[2] . "<br>\n";
    echo $match[3] . "<br>\n";
}
?>
于 2012-04-20T01:15:14.793 回答
0

(\[\d*\].*\[(SUCCESS|FAILURE)\]\s(s-.{3}).*(\r[\d\.]+){0,5})

http://regexr.com?30n45

这个正则表达式应该可以工作。

于 2012-04-20T01:17:17.063 回答
0

又快又脏,但尝试起来很有趣:

$success_pattern = '/\[(SUCCESS|FAILURE)\]/';
preg_match_all($success_pattern, $subject, $success_matches);

$descriptor_pattern = '/\[(SUCCESS|FAILURE)\]\s(.+?):/';
preg_match_all($descriptor_pattern, $subject, $descriptor_matches);

$numbers_pattern = '/\[.*?\n((\d+\.{0,1}\d*\n)+)/';
preg_match_all($numbers_pattern, $subject, $numbers_matches);

$output = array();
for($x = 0; $x < count($success_matches[0]); $x++) {
    $output[] = array(
        'success' => $success_matches[1][$x],
        'descriptor' => $descriptor_matches[2][$x],
        'numbers' => explode("\n", trim($numbers_matches[1][$x]))
    );
}
于 2012-04-20T01:46:31.177 回答
0

怎么样:

$string = '[9] 20:30:40 [SUCCESS] s-a24:1337
2
8.87
4038047
35320
4002727
[10] 20:30:40 [SUCCESS] s-d28:1337
2
2.64
4038047
37363
4000684
[11] 20:30:40 [SUCCESS] s-b29:1337
2
0.56
4038047
33066
4004981
[66] 20:30:42 [FAILURE] s-b25:1337 Timed out, Killed by signal 9
2
1.16
4038047
35274
[67] 20:30:42 [FAILURE] s-b30:1337 Timed out, Killed by signal 9
';

preg_match_all("/\[.*?\[(SUCCESS|FAILURE)\]\s+([^:]+):[^\n]+\n(?:([^[]+?)\n)?(?:([^[]+?)\n)?(?:([^[]+?)\n)?(?:([^[]+?)\n)?(?:([^[]+?)\n)?/m", $string, $matches, PREG_SET_ORDER);
print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => [9] 20:30:40 [SUCCESS] s-a24:1337
2
8.87
4038047
35320
4002727

            [1] => SUCCESS
            [2] => s-a24
            [3] => 2
            [4] => 8.87
            [5] => 4038047
            [6] => 35320
            [7] => 4002727
        )

    [1] => Array
        (
            [0] => [10] 20:30:40 [SUCCESS] s-d28:1337
2
2.64
4038047
37363
4000684

            [1] => SUCCESS
            [2] => s-d28
            [3] => 2
            [4] => 2.64
            [5] => 4038047
            [6] => 37363
            [7] => 4000684
        )

    [2] => Array
        (
            [0] => [11] 20:30:40 [SUCCESS] s-b29:1337
2
0.56
4038047
33066
4004981

            [1] => SUCCESS
            [2] => s-b29
            [3] => 2
            [4] => 0.56
            [5] => 4038047
            [6] => 33066
            [7] => 4004981
        )

    [3] => Array
        (
            [0] => [66] 20:30:42 [FAILURE] s-b25:1337 Timed out, Killed by signal 9
2
1.16
4038047
35274

            [1] => FAILURE
            [2] => s-b25
            [3] => 2
            [4] => 1.16
            [5] => 4038047
            [6] => 35274
        )

    [4] => Array
        (
            [0] => [67] 20:30:42 [FAILURE] s-b30:1337 Timed out, Killed by signal 9

            [1] => FAILURE
            [2] => s-b30
        )

)
于 2012-04-20T09:22:45.520 回答