Regular expressions used to validate custom formats
I have this format: xx:xx:xx or xx
:xx:xx-y
, where x can be 0-9 a-f A-F and y
can only be 0 or 1.
I came up with this regular expression: ([0-9A-Fa-f]{2}[:][0-9A-Fa-f]{2}[:][0-9A-Fa -f]{2}| -][0-1]{1})
(See regexr).
But this also matches 0a:0b:0c-3
, which is not expected.
Is there a way to remove these cases from the results?
Solution
[:]
indicates that the list contains only :
characters. It is with
:
。 The result of [-]
is the same as -
.
In addition, {1}
means “exactly once in the previous paragraph”. It does nothing and you can remove it completely.
To match xx:xx:xx or xx:xx
:xx-y
, the part that matches -y
must be optional. The quantifier ? after the optional
section marks it
as optional.
All in all, your regex
should look like this:
[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}(-[01])?
If you can tell you to use the regex engine to ignore character case, you can remove all character classes from A-F (or a-f
) and
regex
becomes:
[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}(-[01])?
How it works, block by block:
[0-9a-f] # any digit or letter from (and including) 'a' to 'f'
{2} # the previous piece exactly 2 times
: # the character ':'
[0-9a-f]
{2}
:
[0-9a-f]
{2}
( # start a group; it does not match anything
- # the character '-'
[01] # any character from the class (i.e. '0' or '1')
) # end of group; the group is needed for the next quantifier
? # the previous piece (i.e. the group) is optional
# it can appear zero or one times
See the effect in action: https://regexr.com/4rfvr
Update
As mentioned by @the-fourth-bird in the comment, if regex
has to match the entire string, then you need to anchor its end:
^[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}(-[01])?$
^ as the beginning of the first character match string of
regex,
$
as the end of the last character match string. This way regex
only matches the entire string (when there are no
other characters before and after xx:xx:xx or xx:xx:xx- before or after the y
part).
If you use regex
to find xx:xx:xx or xx:xx:xx-y
in a larger string, then you don’t need to add ^
and $
. Of course, you can just add
^
or $
to have regex
match only the beginning or end of the string.