# Matching a time interval using regex

What I needed to do was check whether a given string matches a certain pattern. The pattern is this:

00:00:00,000 --> 00:00:00,000


Things to keep in mind:

The 0s can be numbers from 0 to 9.

The pattern must be alone in a single line; the line has to only consist of the pattern.

I came up with this:

"^(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d) --> (\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)"


I tested it a few times and strings that respect the pattern return true, the ones that don't, return false, as they should.

Here's a test case:

import java.util.regex.Pattern;

public class TestCaseRegex {

public static void main(String[] args) {
//will return true
String testOne = "00:01:23,846 --> 00:01:26,212";

//will return false, there's a letter where a number should be
String testTwo = "00:01:23,84a --> 00:01:21,221";

//will return true
String testThree = "00:05:54,846 --> 00:01:16,450";

//will return false. The string doesn't match the format.
String testFour = "00:05:54,6 --> 00:0116,450";

System.out.println(patternMatch(testOne));
System.out.println(patternMatch(testTwo));
System.out.println(patternMatch(testThree));
System.out.println(patternMatch(testFour));
}

public static boolean patternMatch(String str) {
Pattern p = Pattern.compile("^(\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d) "
+ "--> (\\d\\d):(\\d\\d):(\\d\\d),(\\d\\d\\d)");
return p.matcher(str).matches();
}

}


As I'm very new to regex, I'm wondering if this is the most efficient/correct way to accomplish this.

• Suppose that a string matches the pattern. Then what? What do you do with that information? – 200_success Mar 28 '17 at 5:59
• The idea will be to split the string again by --> and store each time interval separately. But the question is about the regex I came up with, and if it's the most efficient to validate the pattern I presented. I'm not thinking ahead of that (yet). – Morgan Mar 28 '17 at 6:05
• I'm far to be an expert in Regex. But you may have used \d{2} instead of \d\d – gervais.b Mar 28 '17 at 6:08
• @gervais.b Oh that's good. Can I also use \\d{3} for \\d\\d\\d? – Morgan Mar 28 '17 at 6:10
• @Morgan Yes, of course, you can alos go a little deeper by repeating the 00: twice : (\d{2}:){2}. And if your goal is to extract both parts, you can use capturing groups : ((?:\d{2}:){2}\d{2},\d{3}) --> ((?:\d{2}:){2}\d{2},\d{3}) regexplanet.com/cookbook/… – gervais.b Mar 28 '17 at 6:15

Rather than using \d, you might want to limit the matcher to valid ranges.

\d{2}:([0-5]\d:){2},\d{3}


would match either of the timestamps and it would prevent someone from entering

11:88:88,888 --> 12:99:99,999


If you want to check for any valid time interval, you should consider to allow strings like:

0:0:0,0-->12:03:4,344


Try this:

^\s*\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\s*-->\s*\d{1,2}:\d{1,2}:\d{1,2},\d{1,3}\s*\$

• In this case, the example you provided isn't considered a valid time interval. The string needs to contain double digits for H, M, and S and three digits for milliseconds. – Morgan Mar 28 '17 at 9:12