Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

case insenstive for month, in a date not working fine.

Last post 04-26-2011, 10:15 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  04-25-2011, 9:25 AM 81658

    case insenstive for month, in a date not working fine.

    Dear all,

    ^(0?[1-9]|[12][0-9]|3[01]|(?i:un))-(JAN|FEB|FEb|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC(?i:unk))-(19|20)\\d\\d$";
             // "(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-((19|20)\\d\\d)";

    above code work for date pattern 01-JAN-2010 but not for 01-feb-2010  please help me in this   see the code below 

     

    ======================================

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    class DateValidatorOne{
     
    private Pattern pattern;
    private Matcher matcher;
     
    private static final String DATE_PATTERN ="^(0?[1-9]|[12][0-9]|3[01]|(?i:un))-(JAN|FEB|FEb|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC(?i:unk))-(19|20)\\d\\d$";
             // "(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-((19|20)\\d\\d)";
     
      public DateValidatorOne(){
          pattern = Pattern.compile(DATE_PATTERN);
      }
     
      /**
       * Validate date format with regular expression
       * @param date date address for validation
       * @return true valid date fromat, false invalid date format
       */
       public boolean validate(final String date){
     
         matcher = pattern.matcher(date);
     
         if(matcher.matches()){
     
         matcher.reset();
     
         if(matcher.find()){
     
                 String day = matcher.group(1);
             String month = matcher.group(2);
             int year = Integer.parseInt(matcher.group(3));
     
             if (day.equals("31") &&
              (month.equalsIgnoreCase("APR") || month .equalsIgnoreCase("JUN") || month.equalsIgnoreCase("SEP") ||
                      month.equalsIgnoreCase("NOV") || month .equalsIgnoreCase("JUN") ||
                      month.equalsIgnoreCase("SEP"))) {
                return false; // only 1,3,5,7,8,10,12 has 31 days
             } else if (month.equalsIgnoreCase("FEB") || month.equalsIgnoreCase("FEB")) {
                      //leap year
              if(year % 4==0){
                  if(day.equals("30") || day.equals("31")){
                      return false;
                  }else{
                      return true;
                  }
              }else{
                     if(day.equals("29")||day.equals("30")||day.equals("31")){
                      return false;
                     }else{
                      return true;
                  }
              }
              }else{                 
            return true;                 
              }
           }else{
                  return false;
           }         
         }else{
          return false;
         }               
       }
    }
    class DateValidator{
    public static void main(String args[])throws Exception{
       
        DateValidatorOne d=new DateValidatorOne();
        boolean t=d.validate("01-FEb-2010");
        System.out.println("--value is -->"+t);
       
    }    
    }

     ======================================

     

    Filed under: ,
  •  04-26-2011, 2:47 AM 81668 in reply to 81658

    Re: case insenstive for month, in a date not working fine.

    Hi kishor,

    If u can rewrite the above date pattern such that the month is not case sensitive , the problem would be solved.

    change the above date pattern to the below pattern

     private static final String DATE_PATTERN ="^(0?[1-9]|[12][0-9]|3[01]|(?i:un))-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-(19|20)\\d\\d$";

    and after that

    public DateValidatorOne(){
          pattern = Pattern.compile(DATE_PATTERN,Pattern.CASE_INSENSITIVE);
      }

    These two changes are required inorder to solve the problem.

    For further clarification you can reach me on my mobile number 9288056218

    Thanks & Regards,

     

    K.Sudheer Kumar

    Software Engineer

     

  •  04-26-2011, 10:15 PM 81678 in reply to 81658

    Re: case insenstive for month, in a date not working fine.

    Your problem is HOW you are attempting to set the "ignore case" option.

    For example, consider:

    (0?[1-9]|[12][0-9]|3[01]|(?i:un))

    This is interpreted as 4 alternatives that are (in order):
    - match an optional 0 followed by a single digit from "1" to "9"
    - match a "1" or "2" followed by a digit from "0" to "9" (by the way, this last bit can be shorted to '\d')
    - match a "3" followed by a "0" or "1"
    - turn on the "ignore case" option and match the "UN"(or "un" or "Un" or "uN") characters

    In the last alternative, the effect of turning on the "ignore case" option lasts ONLY to the end of the group. Therefore it has no impact on the part of your pattern that matches the month names.

    Within the section that matches the month names, there is the last alternative which is:

    DEC(?i:unk)

    What this does is to try to match the "DEC" characters (uppercase only) which MUST be followed by a case insensitive "unk". I suspect that you are missing an alternation operator between the "C" and the "(" in the part of the pattern. However, the same comment applies in that the setting of the "ignore case" option only lasts as far as the end of the group.

    You could either make the whole pattern case insensitive (as per the previous response to your posting) or use something like:

    ^(0?[1-9]|[12][0-9]|3[01]|(?i:un))-(?i)(JAN|FEB|FEb|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC|unk)-(19|20)\\d\\d$

    By the way, if this is the case you can probably remove the duplication of the "FEB" (and "FEb") alternatives. Note that I have also made the last month name alternative simply '|unk)'

    Susan

View as RSS news feed in XML