Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I want to return the time format HH:MM am|pm (11:45 AM) or any time matches this regex :
String meridian = "^(0[1-9]|[10-12]):(0[1-9]|[11-59])\\s(AM|PM|am|pm)";
But its not working for me.
[10-12]
[11-59]
-
1
1-5
9
[10-12] is equivalent to [1012] which is equivalent to [012]
[1012]
[012]
Similarly [11-59] is equivalent to [123459]
[123459]
Use:
^(0[0-9]|1[0-2]):[0-5][0-9]\\s(AM|PM|am|pm)$
Add a comment
[...] is a set of characters, not numbers.
[...]
To match all numbers between 01 and 12, use (0[1-9]|1[0-2]).
01
12
(0[1-9]|1[0-2])
To match all numbers between 00 and 59, use [0-5][0-9]
00
59
[0-5][0-9]
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
[10-12]and[11-59]do not mean what you think they do. The-in a character group doesn't work for two-digit numbers, so[11-59], means a1or1-5or9.