Regex to allow numbers and only one hyphen in the middle Regex to allow numbers and only one hyphen in the middle php php

Regex to allow numbers and only one hyphen in the middle


Try this one:

/^\d{1,2}(-\d{1,2})?$/

One or two digits, followed by, optionally, ( a hyphen followed by one or two digits)


Fairly easy:

^\d+(-\d+)?$

At least one (+) digit (\d), followed by an optional group containing a hyphen-minus (-), followed by at least one digit again.


For strings containing only that pattern the following should work

^(\d{2}-)?\d{2}$

A group of 2 digits followed by minus ending with a group of 2 digits without minus.