简体   繁体   中英

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.

Try this:

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

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

var_dump($matches);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM