This question already has an answer here:
I am trying to match comma separated numbers, but if any of the numbers is not valid stop regex and don't match it at all.
Here is an example that should be matched
3123123213212,3123123263212,3173123213212
This string shouldn't be matched
3123123213212,3123123263212dad,3173123213212
So at least one invalid number should lead to unmatched regex.
What I've tried is the following expression
(?:(\d+)(?=\,|\s|$))+
Here is Regex101 link
https://regex101.com/r/JpuA5X/1
The problem that even if some number is invalid it matches other numbers, but this is not acceptable.
How can I modify my regex to get desired result ?
Thanks.
UPDATE
Sorry, I haven't mentioned, I need to group every single number.
In this instance, I'm not sure it's possible to match and capture what you want without including some other (undesirable) captures, ones that would include the comma.
Instead you can test the string, and if it matches, split the string into an array using the comma as the delimiter. If the string doesn't match, return an empty array or some other value that you would like to symbolize "no match".
You don't need lookarounds for this. Anchors are sufficient:
Explanation:
You could look for start and end and the parts between.