简体   繁体   中英

Regex: Another way to match structure separated by commas

I want to know if a string is a collection of, by example, numbers ( [0-9] ).

I this case, i'm using the regular expression [0-9](,[0-9])* to find one or more numbers separated by commas (A collection of numbers).

Is there a better way to do it? I mean a shorter expression perhaps.

I would suggest the following pattern:

(?<=^|,|\s)(\d+)

(?<=...) is a lookbehind assertion that will not be captured into the groups nor be included into the matched string. It is used to identify the starting position of the number to be matched.

You can try the above pattern interactively in the following website:

https://regex101.com/r/IKGWtA/1

\\d*(,\\d*)* will catch the situation where you have multiple digits before and after a comma eg 100,000 . This regex will only grab 0,0 from that same number.

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