0

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 %+^-_
            )
Ergec
  • 11,608
  • 7
  • 52
  • 62

2 Answers2

1

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.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

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 ().

Kit
  • 20,354
  • 4
  • 60
  • 103
davide
  • 1,918
  • 2
  • 20
  • 30