Reputation: 11824
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 %+^-_]
)
)
Why result has two array? Anyway it's not a big deal but I wonder.
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
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
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