dimanche 29 décembre 2019

Java conditional regex to check 24 hour time?‽

I am doing the following programming exercise: regex validation of 24 hours time. The statement is:

Write a regex to validate a 24 hours time string. See examples to figure out what you should check for:

Accepted: 01:00 - 1:00

Not accepted:

24:00

You should check for correct length and no spaces.

I would like to ask, why the following regular expression, matches 24:00

public class RegexValidation {
  public static boolean validateTime(String time) {
    System.out.println("time: "+time);
    return time.matches("(?:^[2])[0123]+:[0-5]+[0-9]|[0-9]+:[0-5]+[0-9]");
  }
}

Because I thought it was expressing: "If string starts with 2, then it should continue with [0-3] (one or more) : [0-5] one or more and [0-9]. Else it continues with [0-9] one or more : [0-5] one or more and [0-9]."

The tests are:

import org.junit.Test;
import static org.junit.Assert.*;

public class RegexValidationTest {

  @Test
  public void test1() {
    assertTrue(RegexValidation.validateTime("01:00"));
  }

  @Test
  public void test2() {
    assertTrue(RegexValidation.validateTime("1:00"));
  }

  @Test
  public void test3() {
    assertTrue(RegexValidation.validateTime("00:00"));
  }

  @Test
  public void test4() {
    assertFalse(RegexValidation.validateTime("13:1"));
  }

  @Test
  public void test5() {
    assertFalse(RegexValidation.validateTime("12:60"));
  }

  @Test
  public void test6() {
    assertFalse(RegexValidation.validateTime("24:00"));
  }
}

In addition I wrote the following solution, which passes the tests:

public class RegexValidation {
  public static boolean validateTime/*⌚*/(String time) {
    if(time.matches("[\\d]+:[\\d]{2}")){
      String[] times = time.split(":");
      int hours = Integer.parseInt(times[0]);
      int minutes = Integer.parseInt(times[1]);
      return hours < 24 && minutes < 60;
    }
    return false;
  }
}

I have also read:

Finally the question is, why in the first code the regex matches 24:00?‽

Aucun commentaire:

Enregistrer un commentaire