I have following lines of code
String time = "14:35:59.99";
String timeRegex = "(([01][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])(.([0-9]{1,3}))?";
String hours, minutes, seconds, milliSeconds;
Pattern pattern = Pattern.compile(timeRegex);
Matcher matcher = pattern.matcher(time);
if (matcher.matches()) {
hours = matcher.replaceAll("$1");
minutes = matcher.replaceAll("$4");
seconds = matcher.replaceAll("$5");
milliSeconds = matcher.replaceAll("$7");
}
I am getting hours, minutes, seconds, and milliSeconds using the matcher.replace method and back references of regex groups. Is there any better method to get value of regex groups. I tried
hours = matcher.group(1);
but it throws the following exception:
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:477)
at com.abnamro.cil.test.TimeRegex.main(TimeRegex.java:70)
Am I missing something here?
It works fine if you avoid calling
matcher.replaceAll. When you callreplaceAllit forgets any previous matches.Notice that I’ve also made a couple of improvements to your regular expression:
(?: ... )for the groups that you aren’t interested in capturing..which matches any character to\\.which matches only a dot.See it working online: ideone