Solved

How do I specify one or two days in a date string using regular expression

Posted on 2004-08-26
7
234 Views
Last Modified: 2010-03-31
Hi

I'm using regular expression
("\\[d]{2}/\\d{2}/\\d{4}")
to check a date in this format.
dd/mm/yyyy

I would also like to use it check a date in this format
d/m/yyyy, d/mm/yyyy, dd/m/yyyy

What do I change in the regular expression so it realises date can be either dd or d, mm or m

Thanks
0
Comment
Question by:MelaniePhilpot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11900868
("\\[d]{1,2}/\\d{1,2}/\\d{4}")

?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900875
Sorry:

("\\d{1,2}/\\d{1,2}/\\d{4}")

0
 
LVL 18

Accepted Solution

by:
armoghan earned 50 total points
ID: 11900892
This will solve your problem.
Instead of using Reg expression
use simple date format
http://javaalmanac.com/egs/java.text/FormatDate.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 11900896
or:

    StringTokenizer st = new StringTokenizer( "10/5/1974, 26/8/2004, 3/12/2005, 31/02/2003", "," ) ;
    boolean valid = true ;
    SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" ) ;
    while( st.hasMoreTokens() )
    {
      String tok = st.nextToken().trim() ;
      try
      {
        Date d = sdf.parse( tok ) ;
        System.out.println( tok + " is a valid date " + d ) ;
      }
      catch( ParseException ex )
      {
        System.out.println( tok + " is not a valid date" ) ;
        valid = false ;
        break ;
      }
    }
    System.out.println( valid ) ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900897
whoops...sorry armoghan...  didn't see your post...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11900942
:-(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11912929
MelaniePhilpot, what was wrong with Tim's second answer?
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question