Solved

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

Posted on 2004-08-26
7
191 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

26 Experts available now in Live!

Get 1:1 Help Now