正则表达式 - 匹配括号但将其从结果中排除

时间:2013-09-11 13:34:11

标签: regex

我有这个示例字符串

[can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_][can be anything here %+^-_]

我的模式是(\[[^\]]+\])

我得到了这个结果

(
    [0] => Array
        (
            [0] => [can be anything here %+^-_]
            [1] => [can be anything here %+^-_]
            [2] => [can be anything here %+^-_]
            [3] => [can be anything here %+^-_]
            [4] => [can be anything here %+^-_]
            [5] => [can be anything here %+^-_]
            [6] => [can be anything here %+^-_]
            [7] => [can be anything here %+^-_]
        )

    [1] => Array
        (
            [0] => [can be anything here %+^-_]
            [1] => [can be anything here %+^-_]
            [2] => [can be anything here %+^-_]
            [3] => [can be anything here %+^-_]
            [4] => [can be anything here %+^-_]
            [5] => [can be anything here %+^-_]
            [6] => [can be anything here %+^-_]
            [7] => [can be anything here %+^-_]
        )

)

问题1

为什么结果有两个数组?无论如何,这不是什么大问题,但我想知道。

问题2

如何仅使用正则表达式删除每个数组值的开头和末尾的括号。像这样。

[0] => Array
            (
                [0] => can be anything here %+^-_
                [1] => can be anything here %+^-_
                [2] => can be anything here %+^-_
                [3] => can be anything here %+^-_
                [4] => can be anything here %+^-_
                [5] => can be anything here %+^-_
                [6] => can be anything here %+^-_
                [7] => can be anything here %+^-_
            )

2 个答案:

答案 0 :(得分:1)

为什么结果有两个数组?

因为在括号中加上一些东西(())会为它分配一个组号,这就是组1的含义。第0组是你的整场比赛。有关详细信息,请参阅this

如何在每个数组值的开头和结尾删除括号?

将正则表达式更改为:

\[([^\]]+)\]

对于上述情况,匹配的[]位于()之外。这将使第1组成为你想要的。

为了使第0组成为您想要的,没有第1组,您必须使用look-around

(?<=\[)[^\]]+(?=\])

但这在很大程度上是不必要的。

(?<=\[)是肯定的后卫,检查前一个字符是[,但不会在匹配中包含它。
(?=\])是正向前瞻,检查下一个字符是],但不会在匹配中包含它。

答案 1 :(得分:0)

只需将大括号从捕获组中删除:

\[([^\]]+)\]

您有两个数组,因为一个是正则表达式的完全匹配,另一个是()捕获的数组。