简体   繁体   中英

PHP regex and repeated patterns

I'd like to match these strings using preg_match, basicaly only repeated patterns of digits and a comma (optional), no letters

123
123,
123,456
123,456,
123,456,789
123,456,789,
etc...

but not

abc
123,abc
123,abc,456
abc,123,456

thanks

Put comma and the pattern to match one or more digits inside a non-capturing group and then make it to repaet zero or more times. And also don't forget to add an optional comma at the last.

^[0-9]+(?:,[0-9]+)*,?$

DEMO

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