-4

I need to use a regex to validate the next rules :

•The first 8 characters should be in ‘date’ format of YYYYMMDD without separators

•Total expression length should be 13 characters

I use internal regular expressions manager

Java code example :

import java.util.regex.Pattern;

public class TestDate {
  public static final Pattern datePattern = Pattern.compile("** Need RegEx **");

  public static void main(String[] args) {
    System.out.println(datePattern.matcher("1996022965789").matches()); 
// -Wrong value 
 System.out.println(datePattern.matcher("1996022865789").matches()); 
// -Correct value 
  }
}

Examples for input values and expected results :

Input: 201909866666 Result: Wrong Length

Input: 2019022966666 Result: Wrong date value

Input: 2019022866666 Result: correct

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
  • Did you tried some regex yourself? Do you want to differentiate between "Wrong Length" and "Wrong date value", because your code has only two states - ok or wrong? Also I do not think regular expression is able to know when 29th February is ok or not, you will need to use some date API to check it. – Piro says Reinstate Monica Sep 12 '19 at 11:40
  • You can try site like [regxr](https://regexr.com/) to visually see the matching pattern while, trying to form regular expression. – Laxman Sep 12 '19 at 12:20
  • 1
    Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Sep 12 '19 at 12:47
  • Don’t use a regular expression for validating a date. It’ll be much too complicated. Use `LocalDate` and a `DateTimeFormatter` for validating the date and possibly a regular expression for the last 5 digits. – Ole V.V. Sep 12 '19 at 15:41

1 Answers1

0

Don’t use a regular expression for validating a date. It’ll be much too complicated. Use LocalDate and a DateTimeFormatter. Here’s a solution that doesn’t use any regular expression at all. You may also use a regular expression for checking that the 5 characters after the date are digits if you prefer.

public void testDate(String dateString) {
    if (dateString.length() == 13) {
        // Check date part, first 8 digits
        try {
            LocalDate.parse(dateString.substring(0, 8), DateTimeFormatter.BASIC_ISO_DATE);
            boolean allDigits = dateString.chars().allMatch(Character::isDigit);
            if (allDigits) {
                System.out.println(dateString + ": correct");
            } else {
                System.out.println(dateString + ": wrong character, must be digits");
            }
        } catch (DateTimeParseException dtpe) {
            System.out.println(dateString + ": invalid date value");
        }
    } else {
        System.out.println(dateString + ": wrong length");
    }
}

Let’s try it out:

    testDate("1996022965789");
    testDate("1996022865789");
    testDate("201909866666");
    testDate("2019022966666");

Output is:

1996022965789: correct
1996022865789: correct
201909866666: wrong length
2019022966666: invalid date value

For the first result, 1996 was a leap year, so February 29 did exist that year. The last result shows invalid date value because 2019 is not a leap year.

LocalDate.parse throws a DateTimeParseException if the date is invalid, which we catch and use for printing the correct message in this case. You may polish the messages if you like; as they stand, they may be too short and not very user-friendly.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117