Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about regular expression

 
Sasha McGrath
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I make a regular expression that would represent the following date: 2008-09-17 ?
 
Shashank Agarwal
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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}.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic