aspose 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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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: 36453
    
  15
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
 
Similar Threads
Check for String
How can I add error handling facilities to this code?
Validations for name in text only
Stripping characters from a string
How to use Regular expression in Jsp