2

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.

2
  • 2
    [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 a 1 or 1-5 or 9. Commented Jul 20, 2012 at 13:44
  • 4
    Why do you want to use regular expressions rather than simply trying to parse the value? Commented Jul 20, 2012 at 13:45

2 Answers 2

4

[10-12] is equivalent to [1012] which is equivalent to [012]

Similarly [11-59] is equivalent to [123459]

Use:

^(0[0-9]|1[0-2]):[0-5][0-9]\\s(AM|PM|am|pm)$
Sign up to request clarification or add additional context in comments.

1 Comment

how to modify the regex then ?
1

[...] is a set of characters, not numbers.

To match all numbers between 01 and 12, use (0[1-9]|1[0-2]).

To match all numbers between 00 and 59, use [0-5][0-9]

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.