Lester Peabody
Lester Peabody

Reputation: 1888

Matching an unknown number of groupings in a regular expression in PHP

I need a regular expression that can match an unknown number of groupings in PHP.

For example, say I have the string 23434_234_234_234234_234_2342_234. I need my match array to contain each grouping. The number of groupings can range from 1 to potentially infinity.

Yes, I realize this could be done by just chopping up the string and using the underscore as the separator, but this is an exercise in regular expressions, not string manipulation.

Upvotes: 0

Views: 249

Answers (1)

Paul S.
Paul S.

Reputation: 1537

Try this:

$string = '23434_234_234_234234_234_2342_234';
$pattern = '/([0-9]+)/';

preg_match_all($pattern, $string, $matches);

var_dump($matches);

Upvotes: 1

Related Questions