Solved

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

Posted on 2004-08-26
7
199 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now