I have a filename i need to validate using preg_match in PHP, but i dont know much about regex patterns to fix my issue.

The string i am trying to validate is 2 numbers (0-9), a comma, 2 numbers (0-9), a hyphen, 2 numbers (0-9), a comma, 2 numbers (0-9)

An example is 47,60-51,00

I have tried the following

(-)?[0-9]+((,|-)[0-9]+)*

and

^[0-9][0-9,-]-[0-9,-][0-9]$

But both seem to fail one way or another. Could someone help point me in the right direction

1 Answers

4
The fourth bird On Best Solutions

You want to match the comma as a separate match but adding it to a character class [0-9,-] it can also match a digit or -

To match that format in a more specific way, you could use this pattern with a quantifier {2} to repeat matching a digit and match the comma's and hyphen at the expected position.

^[0-9]{2},[0-9]{2}-[0-9]{2},[0-9]{2}$

Regex demo

If you want the match to start with an optional hyphen you could start the pattern with ^-?