How about
[0-9]?[0-9]:[0-9]?[0-9] AM|PM
Assuming your times are always in the form (x)x:(x)x AM
or (x)x:(x)x PM
For clarification:
[0-9] matches any digit from 0-9
? matches 0 or 1 occurences
x|y matches x or y
But, as others have pointed out, if the string is always the same then it is better to use substring etc. With a regex it gets much more complicated than it needs to be.
Edit: to find it between dog
and done
do
Dog.*([0-9]?[0-9]:[0-9]?[0-9] AM|PM).*done
and use \1
to get the matched time or make a substring between Dog
and done
and use the first regex.
Edit2: I added a working example:
public static void main (String[] args) {
String in = "The Big Cat eats at 3:49 AM and the Big Dog eats Daily (BBB) , On 12 at 5:00 AM done";
Pattern pattern = Pattern.compile("Dog.*([0-9]?[0-9]:[0-9]?[0-9] AM|PM).*done");
Matcher matcher = pattern.matcher(in);
System.out.println("matching");
while(matcher.find()) {
System.out.println(matcher.group(1) + "");
}
}
Output:
matching
5:00 AM