Solved

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

Posted on 2004-08-26
7
225 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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