0

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

7
  • 1
    [0-9]+(?:,[0-9]*)*? Demo: regex101.com/r/iR3bC3/1 Commented Jul 24, 2015 at 14:07
  • 1
    What do you mean exactly by "repeated pattern"? Show us other possible patterns. Commented Jul 24, 2015 at 14:07
  • the repeated pattern is \n+\,*. when there's a letter I need preg_match to return false Commented Jul 24, 2015 at 14:14
  • @stachu i think so to validate string ^([0-9]+)(?:,([0-9]+))*$ Commented Jul 24, 2015 at 14:16
  • @splash58 this one won't work for "123,456," Commented Jul 24, 2015 at 14:18

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.