Reputation: 750
Hello i have a regular expression that works [2-5][0-5]:[0-8][0-8]
accepts numbers like 20:88, 35:14, 32:54, etc.
I use the anotattion javax.validation.constraints.Pattern
to validate entities.
But I need to accept empty string or the format that I showed.
I tried [^.]|[2-5][0-5]:[0-8][0-8]
[^.]|([2-5][0-5]:[0-8][0-8])
^.|[2-5][0-5]:[0-8][0-8]
(^.)|[2-5][0-5]:[0-8][0-8]
But not works. I tried put empty|myformat
Upvotes: 1
Views: 101
Reputation: 89547
Try this:
^(?:[2-5][0-5]:[0-8][0-8])?$
(?:..)
is a non capturing group and ?
makes the group optional.
^
and $
are anchors for start and end of the string.
However, if your goal is to match a number between 20 and 55 for the first part and, 00 and 88 for the second part, then this pattern will do it better:
^(?:(?:[2-4][0-9]|5[0-5]):(?:[0-7][0-9]|8[0-8]))?$
Upvotes: 4