• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about regular expression

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I make a regular expression that would represent the following date: 2008-09-17 ?
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic