I have a regular expression like this (much simplified):
^(ab)*$
And am matching against this:
abababababababab
When I run it through preg_match:
preg_match('/$(ab)*$/', 'abababababababab', $matches); print_r($matches);
I get this:
Array ( [0] => abababababababab [1] => ab )
Wheras I expect this:
Array ( [0] => abababababababab [1] => ab [2] => ab [3] => ab [4] => ab [5] => ab [6] => ab [7] => ab [8] => ab }
How can I get what I expect?
(Note that the subexpression could be more complicated, e.g. ([aA][bB]), and I want the matched expressions in their order in the subject.)
Do it by using
preg_match_alllike this:Remember that
preg_matchonly provides the first match and thatpreg_match_allhas the same functionality, but returns all. Also note that now the regular expression has changed. If you use the asterisk, it’ll likely consume the entire string on the first match. You could, however, try something like(ab)*?to make it nongreedy.