File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about regular expression" Watch "Question about regular expression" New topic
Author

Question about regular expression

Sasha McGrath
Greenhorn

Joined: Jun 01, 2007
Posts: 10
How would I make a regular expression that would represent the following date: 2008-09-17 ?
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Originally posted by Sasha McGrath:
How would I make a regular expression that would represent the following date: 2008-09-17 ?




Note that 1, 2 allows matching dates like 2008-10-3. If it's definitely going to be 2 digit, then use {2, 2}.
Sasha McGrath
Greenhorn

Joined: Jun 01, 2007
Posts: 10
Thank you for answering my question.

Suppose I had the following code:

String input = new String("2008-09-17T00:00:00-04:00");
Pattern datePattern = Pattern.compile("\\d{4,4}-\\d{1,2}-\\d{1,2}");
Matcher myMatch = datePattern.matcher(input);

How would I find the string that matches the pattern and store it in a variable?

[ October 02, 2008: Message edited by: Sasha McGrath ]
[ October 02, 2008: Message edited by: Sasha McGrath ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Have a look at the API documentation of the method find() in class Matcher. It explains how you can find a substring in a string that matches the specified pattern.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by Shashank Agarwal:
Note that 1, 2 allows matching dates like 2008-10-3. If it's definitely going to be 2 digit, then use {2, 2}.

Or even easier, just {2}. Just like {4,4} is equal to {4}.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Originally posted by Shashank Agarwal:
If it's definitely going to be 2 digit, then use {2, 2}.


There is a subtle pitfall there; you need to write {2,2} not {2, 2} (although Rob has provided a better method!). I used to have no end of trouble before I realised the whitespace in the {} causes errors.
[ October 03, 2008: Message edited by: Campbell Ritchie ]
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Originally posted by Campbell Ritchie:


There is a subtle pitfall there; you need to write {2,2} not {2, 2} (although Rob has provided a better method!). I used to have no end of trouble before I realised the whitespace in the {} causes errors.

[ October 03, 2008: Message edited by: Campbell Ritchie ]


Did not know that. You probably saved me hours.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about regular expression