Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

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

Thursday

(?i) and (?-i) in regex means

(?i) turns on case-insensitive mode, (?-i) turns it off.

For example:
For regex (?i)ed(?-i)a 
 - matches with the following words: eda, Eda, eDa, EDa
 - does not match with the following words: edA, EdA, eDA, EDA

Tuesday

java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2

BUG:
I have the following String:
           String keyword = '\AMore\s*Code\Z';
and i want to replace regexs as following:
             keyword.replace("[\\A", "").replace("[\\Z", "").replace("[\\s*", " ") : "";
But i got the following error:
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2

FIX:
Use the java.util.regex.Pattern to escape special characters in regex:
  keyword = Pattern.compile("\\\\A").matcher(keyword).replaceAll(" ");