I have the following regex:
\[([^ -\]]+)( - ([^ -\]]+))+\]
This match the following successfully:
[abc - def - ghi - jkl]
BUT the match is:
Array
(
[0] => [abc - def - ghi - jkl]
[1] => abc
[2] => - jkl
[3] => jkl
)
What I need is something like this:
Array
(
[0] => [abc - def - ghi - jkl]
[1] => abc
[2] => - def
[3] => def
[4] => - ghi
[5] => ghi
[6] => - jkl
[7] => jkl
)
I'm able to do that in C# looking at the groups "captures". How can I do that in PHP?
-
in the character class specifies a range, and your expression' -\]'
means any character from\x20
to\x5D
. Thus[^ -\]]
is the same thing as[^ !"#$%&'()*+,\-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\]]
. You need to escape the dash! – Camel