0

我写正确的reg exp有一点问题,我有一个字符串

2013-01-11 17:38:02.137759 some text leater

或者

11 Jan 17:38:02 some text leater

所以我写了一个正则表达式

(?:(?:(?:\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}(?:\.\d+)?)\s(?:[^\s]+)\s(?:.\d+.)?\s?))?((?:(\d{4}-\d{2}-\d{2})\s\d{2}:\d{2}:\d{2}(?:\.\d+)?)|(?:(\w{3}\s+\d+)\s\d{2}:\d{2}:\d{2})) 

示例结果是:

Array
(
    [0] => Array
        (
            [0] => Jan 11 17:38:02
        )

    [1] => Array
        (
            [0] => Jan 11 17:38:02
        )

    [2] => Array
        (
            [0] => 
        )

    [3] => Array
        (
            [0] => Jan 11
        )

)

或者

Array
(
    [0] => Array
        (
            [0] => 2013-01-11 17:38:02.137759
        )

    [1] => Array
        (
            [0] => 2013-01-11 17:38:02.137759
        )

    [2] => Array
        (
            [0] => 2013-01-11
        )

    [3] => Array
        (
            [0] => 
        )

)

问题是我想跳过一个空字段结果的结果,我该怎么做?

感谢帮助

4

1 回答 1

1

最简单的方法就是array_filter在结果上使用:

array_filter($array, function ($piece) { return array_filter($piece); });
于 2013-02-07T18:10:01.790 回答