I have the following regex:
\\[([^ -\\]]+)( - ([^ -\\]]+))+\\]
This match the following successfully:
[abc - def - ghi - jk
SPL preg_match_all will return regex groups starting on index 1 of the $matches variable. If you want to get only the second group you can use $matches[2] for example.
Syntax:
$matches = array();
preg_match_all(\
'/(He)\w+ (\w+)/',
"Hello world\n Hello Sunshine",
$matches
);
var_dump($matches);
Result:
array(3) {
[0] =>
array(2) {
[0] =>
string(11) "Hello world"
[1] =>
string(14) "Hello Sunshine"
}
[1] =>
array(2) {
[0] =>
string(2) "He"
[1] =>
string(2) "He"
}
[2] =>
array(2) {
[0] =>
string(5) "world"
[1] =>
string(8) "Sunshine"
}
}
P.S. This answer is posted for the context of the question title after being directed here by a Google search. This was the information I was interested in when searching for this topic.