I have two regular expressions that I use to validate Colorado driver’s license formats.
[0-9]{2}[-][0-9]{3}[-][0-9]{4}
and
[0-9]{9}
We have to allow for only 9 digits but the user is free to enter it in as 123456789 or 12-345-6789.
Is there a way I can combine these into one? Like a regex conditional statement of sorts? Right now I am simply enumerating through all the available formats and breaking out once one is matched. I could always strip the hyphens out before I do the compare and only use [0-9]{9}, but then I won’t be learning anything new.
For a straight combine,
or to merge the logic (allowing dashes in one position without the other, which may not be desired),
(The brackets around the hyphens in your first regex aren’t doing anything.)
Or merging the logic so that both hyphens are required if one is present,
(Your
[0-9]s can also be replaced with\ds.)