Search code examples
javaregexdatevalidation

Date validation with regex


I've already asked a question about it here, so I decided to do it from scratch one more time. I am trying to validate dd-MMM-yyyy format but for some reason I always get an error.

Here is my code:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 public class Review1_Main{

 private static Pattern DATE_VALIDATION_PATTERN =
      Pattern.compile("^(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))" +
                      "\\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|" +
                      "Nov|Dec)\\-\\d{4}$");

 public static boolean validate(final String date){

 Matcher matcher = DATE_VALIDATION_PATTERN.matcher(date);

 if(matcher.matches()){

   matcher.reset();

   if(matcher.find()){

     String dd = matcher.group(1);
     String mm = matcher.group(2);
     int yy = Integer.parseInt(matcher.group(3));

     if (dd.equals("31") &&  (mm.equals("Apr") || mm.equals("Jun") ||
                              mm.equals("Sep") || mm.equals("Nov"))) {
       return false;
     } else if (mm.equals("Feb")) {

       if(yy % 4==0){
         if(dd.equals("30") || dd.equals("31")){
           return false;
         }else{
          return true;
         }
       }else{
         if(dd.equals("29")||dd.equals("30")||dd.equals("31")){
           return false;
         }else{
           return true;
         }
       }
     }else{
       return true;
     }
   }else{
     return false;
   }
 }else{
   return false;
 }
}

public static void main(String[] args) {

 Scanner s = new Scanner(System.in);
 System.out.print("Enter a date: ");
   String input = s.nextLine();
 System.out.println(validate(input));
}
}

And here is an error I get:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Integer.java:542)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Review1_Main.validate(Review1_Main.java:21)
    at Review1_Main.main(Review1_Main.java:57)

I understand that this error means that my input = null, but I can't fix it. Any advice would be good. I really want to understand it!

Thank you!

Finally found the answer:

private static Pattern DATE_VALIDATION_PATTERN = Pattern.compile("(0?[1-9]|[12][0-9]|3[01])-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|" +
                  "Nov|Dec)-((19|20)\\d\\d)");

Solution

  • Basically, the problem is that you are attempting to use a capture group in the regex that didn't match anything in the input string.

    I'm not exactly sure, but the either the 3rd capture group is ([0][1-9]) or ([1-2][0-9]), or there isn't one at all. I note that you don't have any round brackets around the "year" section of your regex, so that can't be a capture group under any interpretation!

    UPDATE - according to this Q&A: How are nested capturing groups numbered in regular expressions? - capture groups can be nested, and they are numbered according to the order of the respective group's opening bracket.

    That means that capture group 3 in your regex is the optional ([0][1-9]) group.