I am trying to write a regular expression to allow numbers and only one hypen in the middle (cannot be at start or at the end)
02are acceptable but
02-04-06 are unacceptable
I tried something like this but this would allow – at the beginning and also allow multiple –
I am not that good with regex so a little explanation would be real helpful.
EDIT: Sorry to bug you again with this but I need the numbers to be of only 2 digits (123-346) should be considered invalid.
Try this one:
One or two digits, followed by, optionally, ( a hyphen followed by one or two digits)
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
A group of 2 digits followed by minus ending with a group of 2 digits without minus.