Ergec
Ergec

Reputation: 11824

regex - match brackets but exclude them from results

I have this example string

[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 %+^-_]

My pattern is (\[[^\]]+\])

An I get this as a result

(
    [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 %+^-_]
        )

)

Problem 1

Why result has two array? Anyway it's not a big deal but I wonder.

Problem 2

How can I get rid of brackets at the beginning and at the end of each array value, using only regex. Like this.

[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 %+^-_
            )

Upvotes: 0

Views: 357

Answers (2)

davide
davide

Reputation: 1948

Just keep the braces out of your capturing group:

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

You have two arrays because one is the full match of your regex and the other what has been captured by the ().

Upvotes: 0

Bernhard Barker
Bernhard Barker

Reputation: 55589

Why does the result have two arrays?

Because putting something in parenthesis (()) assigns it a group number, that's what group 1 is. Group 0 is your entire match. See this for more info.

How can I get rid of brackets at the beginning and at the end of each array value?

Change your regex to:

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

For the above, the matching [] are outside of the (). This will make group 1 what you want.

To make group 0 what you want, with no group 1, you'll have to use look-around:

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

But this is largely unnecessary.

(?<=\[) is positive look-behind, checking that the previous character is a [, but doesn't include it in the match.
(?=\]) is positive look-ahead, checking that the next character is a ], but doesn't include it in the match.

Upvotes: 1

Related Questions