java regex question

I am using java regex to validate a date in the format of mm/dd/yyyy and it should also accept m/d/yyyy.
The issue I am having is that I discovered that it wasn't accepting a zero in front of a month or a day.  I played around with the regex and I got it to accept a zero for the day but I can't figure it out for the month.  I tried many different combinations and nothing has worked.  Can someone look at the regex and tell me how I can get it to accept the zero for the month.  I got this regex from the internet and it works great except for the zero issue.

date_validate = "^(?:(31)(\\D)(0?[13578]|1[02])\\2|(29|30)(\\D)(0?[13-9]|1[0-2])\\5|(0?[1-9]|1\\d|2[0-8])(\\D)(0?[1-9]|1[0-2])\\8)((?:1[6-9]|[2-9]?\\d)|\\d{2})$|^(29)(\\D)(0?2)\\12((?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)$";
bolts123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
That looks more like dd/mm/yy
What dates are not validating for you?
0
Bart CremersJava ArchitectCommented:
A question. Do you need to do this using regular expressions? There are better ways available in Java to validate dates.
0
zzynxSoftware engineerCommented:
This code gives as output

Date <01/01/2008> is valid
Date <1/1/2008> is valid
Date <01/1/2008> is valid
Date <1/01/2008> is valid
Date <15/01/2008> is invalid

which is what you want, I guess.
    public static void main(String args[]) {
       
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        df.setLenient(false);
        
        String dates[] = { "01/01/2008", "1/1/2008", "01/1/2008", "1/01/2008" , "15/01/2008" };
        
        for (int i=0, m=dates.length; i<m; i++) {
                try {
                    df.parse(dates[i]);
                    System.out.println("Date <" + dates[i] + "> is valid");
                } catch (ParseException x) {
                    System.out.println("Date <" + dates[i] + "> is invalid");                    
                }
        }
    }

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

zzynxSoftware engineerCommented:
Oh, I forgot:
import java.text.*;

Open in new window

0
bolts123Author Commented:
yes I do need to do regular expression.  I realize there are probably easier ways but I do need to use this regular expression.
0
zzynxSoftware engineerCommented:
Why, if I'm allowed to ask?
0
bolts123Author Commented:
that is what i have been tasked to do
0
gatorvipCommented:
You can find a valid regex here

http://www.regular-expressions.info/dates.html

There's only a few minor adjustments you need to make.
0
bolts123Author Commented:
I am having trouble finding where the month is exactly within my regex.  I have been playing with it for days but I can't find which string contains the month.  I have made adjustments within several places with no luck.
0
ozoCommented:
The regular expression:

(?-imsx:^(?:(31)(\D)(0?[13578]|1[02])\2|(29|30)(\D)(0?[13-9]|1[0-2])\5|(0?[1-9]|1\d|2[0-8])(\D)(0?[1-9]|1[0-2])\8)((?:1[6-9]|[2-9]?\d)|\d{2})$|^(29)(\D)(0?2)\12((?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:16|[2468][048]|[3579][26])00)$)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      31                       '31'
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    (                        group and capture to \2:
----------------------------------------------------------------------
      \D                       non-digits (all but 0-9)
----------------------------------------------------------------------
    )                        end of \2
----------------------------------------------------------------------
    (                        group and capture to \3:
----------------------------------------------------------------------
      0?                       '0' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
      [13578]                  any character of: '1', '3', '5', '7',          This is Jan, Mar, May, July, Aug of when day is 31
                               '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      [02]                     any character of: '0', '2'   this is Oct, Dec when day is 31
----------------------------------------------------------------------
    )                        end of \3
----------------------------------------------------------------------
    \2                       what was matched by capture \2
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (                        group and capture to \4:
----------------------------------------------------------------------
      29                       '29'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      30                       '30'
----------------------------------------------------------------------
    )                        end of \4
----------------------------------------------------------------------
    (                        group and capture to \5:
----------------------------------------------------------------------
      \D                       non-digits (all but 0-9)
----------------------------------------------------------------------
    )                        end of \5
----------------------------------------------------------------------
    (                        group and capture to \6:
----------------------------------------------------------------------
      0?                       '0' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
      [13-9]                   any character of: '1', '3' to '9'   This is Jan and Mar to Sep when day is 29 or 30
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      [0-2]                    any character of: '0' to '2'    This is Oct to Dec when day is 29 or 30
----------------------------------------------------------------------
    )                        end of \6
----------------------------------------------------------------------
    \5                       what was matched by capture \5
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (                        group and capture to \7:
----------------------------------------------------------------------
      0?                       '0' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
      [1-9]                    any character of: '1' to '9'         This is Jan to Oct when day is 1 to 28
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      \d                       digits (0-9)
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      2                        '2'
----------------------------------------------------------------------
      [0-8]                    any character of: '0' to '8'
----------------------------------------------------------------------
    )                        end of \7
----------------------------------------------------------------------
    (                        group and capture to \8:
----------------------------------------------------------------------
      \D                       non-digits (all but 0-9)
----------------------------------------------------------------------
    )                        end of \8
----------------------------------------------------------------------
    (                        group and capture to \9:
----------------------------------------------------------------------
      0?                       '0' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
      [1-9]                    any character of: '1' to '9'      THis is Jan to Sep when day is 1 to 28
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      [0-2]                    any character of: '0' to '2'    This is Oct to Dec when day is 1 to 28
----------------------------------------------------------------------
    )                        end of \9
----------------------------------------------------------------------
    \8                       what was matched by capture \8
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \10:
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      [6-9]                    any character of: '6' to '9'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [2-9]?                   any character of: '2' to '9' (optional
                               (matching the most amount possible))
----------------------------------------------------------------------
      \d                       digits (0-9)
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \d{2}                    digits (0-9) (2 times)
----------------------------------------------------------------------
  )                        end of \10
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \11:
----------------------------------------------------------------------
    29                       '29'
----------------------------------------------------------------------
  )                        end of \11
----------------------------------------------------------------------
  (                        group and capture to \12:
----------------------------------------------------------------------
    \D                       non-digits (all but 0-9)
----------------------------------------------------------------------
  )                        end of \12
----------------------------------------------------------------------
  (                        group and capture to \13:
----------------------------------------------------------------------
    0?                       '0' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    2                        '2'            This is Feb when day is 29
----------------------------------------------------------------------
  )                        end of \13
----------------------------------------------------------------------
  \12                      what was matched by capture \12
----------------------------------------------------------------------
  (                        group and capture to \14:
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      1                        '1'
----------------------------------------------------------------------
      [6-9]                    any character of: '6' to '9'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [2-9]                    any character of: '2' to '9'
----------------------------------------------------------------------
      \d                       digits (0-9)
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      0                        '0'
----------------------------------------------------------------------
      [48]                     any character of: '4', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [2468]                   any character of: '2', '4', '6', '8'
----------------------------------------------------------------------
      [048]                    any character of: '0', '4', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [13579]                  any character of: '1', '3', '5', '7',
                               '9'
----------------------------------------------------------------------
      [26]                     any character of: '2', '6'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      16                       '16'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [2468]                   any character of: '2', '4', '6', '8'
----------------------------------------------------------------------
      [048]                    any character of: '0', '4', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [3579]                   any character of: '3', '5', '7', '9'
----------------------------------------------------------------------
      [26]                     any character of: '2', '6'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
    00                       '00'
----------------------------------------------------------------------
  )                        end of \14
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------


Note that it only allows a  4 digit year on 29/02
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Sorry, the first This is Jan to Oct when day is 1 to 28 should not be there
0
bolts123Author Commented:
Thanks for a great breakdown.  I am playing around with it and seeing if I can get mine to work
0
bolts123Author Commented:
I am still having trouble getting it to accept a 0 for the month.  It is driving me crazy.  Any suggestions?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.