aspose file tools
The moose likes Beginning Java and the fly likes Question about regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

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: 19216

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
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32671
    
    4
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Question about regular expression
 
Similar Threads
How to use Regular expression in Jsp
Check for String
Validations for name in text only
How can I add error handling facilities to this code?
Stripping characters from a string