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"; 


    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.

  • \$\begingroup\$ Suppose that a string matches the pattern. Then what? What do you do with that information? \$\endgroup\$ Commented Mar 28, 2017 at 5:59
  • \$\begingroup\$ 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). \$\endgroup\$
    – Morgan
    Commented Mar 28, 2017 at 6:05
  • 1
    \$\begingroup\$ I'm far to be an expert in Regex. But you may have used \d{2} instead of \d\d \$\endgroup\$
    – gervais.b
    Commented Mar 28, 2017 at 6:08
  • \$\begingroup\$ @gervais.b Oh that's good. Can I also use \\d{3} for \\d\\d\\d? \$\endgroup\$
    – Morgan
    Commented Mar 28, 2017 at 6:10
  • 1
    \$\begingroup\$ @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/… \$\endgroup\$
    – gervais.b
    Commented Mar 28, 2017 at 6:15

2 Answers 2


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


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:


Try this:

  • \$\begingroup\$ 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. \$\endgroup\$
    – Morgan
    Commented Mar 28, 2017 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.