Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
[0-9]+(?:,[0-9]*)*
^([0-9]+)(?:,([0-9]+))*$
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
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
[0-9]+(?:,[0-9]*)*? Demo: regex101.com/r/iR3bC3/1^([0-9]+)(?:,([0-9]+))*$