Friday

Regex date format validation (11_digit_identification_number date_of_birth_with_following_format_dd.mm.yyyy)

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

public class Test {

       public static void main( String args[] ) {

              String line[] = {"96325587410 10.01.1980", "96325587410 8.4.1988",
                          "96325587410 31.12.2000", "96325587410 70.01.2000",                                  "96325587410 1-01-1980", "216235456242334458 10.01.1980"
                          "21652334458 10.01.2980"};

              String pattern = "\\A\\s*\\d{11}\\s*(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.((19|20)\\d\\d)\\s*\\Z";
              String parameters  = "\\A\\s*(\\d{11})\\s*((0?[1-9]|[12][0-9]|3[01]).(0?[1-9]|1[012]).((19|20)\\d\\d))\\s*\\Z";

              for(int i = 0; i< line.length ; i++) {
                     Pattern r = Pattern.compile(pattern);
                     Matcher m = r.matcher(line[i]);
                     System.out.println("KEYWORD: " + line[i]);
                    
                     if (m.find( )) {
                           Pattern cno = Pattern.compile(parameters);
                           Matcher m1 = cno.matcher(line[i]);
                           if(m1.find( )) {
                                  System.out.println("--> identityno: " + m1.group(1));
                                  System.out.println("--> dateofbirth: " + m1.group(2) );
                           }
                          
                     } else {
                           System.out.println("---> NO MATCH");
                     }
                    
                     System.out.println("---------------------------------------");
              }

       }
}


OUTPUT:

KEYWORD: 96325587410 10.01.1980
--> identityno: 96325587410
--> dateofbirth: 10.01.1980
---------------------------------------------
KEYWORD: 96325587410 8.4.1988
--> identityno: 96325587410
--> dateofbirth: 8.4.1988
---------------------------------------------
KEYWORD: 96325587410 31.12.2000
--> identityno: 96325587410
--> dateofbirth: 31.12.2000
---------------------------------------------
KEYWORD: 96325587410 70.01.2000
---> NO MATCH
---------------------------------------------
KEYWORD: 96325587410 1-01-1980
---> NO MATCH
---------------------------------------------
KEYWORD: 216235456242334458 10.01.1980
---> NO MATCH
---------------------------------------------
KEYWORD: 21652334458 10.01.2980
---> NO MATCH
---------------------------------------------

No comments:

Post a Comment