I'm trying to find a time in a String using a regex in Java. Here's the regex:
\d{1,2}?:\d\d(?)(am|pm)??
That should look for 1 or 2 digits, followed by a colon, two more digits, then either "am" or "pm" (irrespective of case).
It mostly works, but if I wrap that entire regex in a capture group, I only get hh:mm. For example, "12:34am" returns just "12:34". No "am".
UPDATE: Full code example
Pattern p = Pattern.compile("\\d{1,2}?:\\d\\d(?)(am|pm)??");
Matcher matcher = p.matcher("12:34AM");
Assert.assertTrue(matcher.find());
Assert.assertEquals("12:34AM", matcher.group());
Anyone have any idea why?
\d{1,2}?:\d\d(am|pm)or if the am/pm is optional,\d{1,2}:\d\d(am|pm)?(?)?\d{1,2}:\d\d(?:am|pm)