• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

regex date capture - greed, reluctance, and precedence problem

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I've got a long list of strings with dates in the format dd-mmm-yyyy which I'm trying to capture. I'd like to be able to handle missing leading zeroes in the day part (i.e. properly capture 01-Jan-2011 and 1-Jan-2011).

My current code doesn't handle leading zeroes.



I thought by changing the day part to ".*((?:[12][0-9]|3[01]|0?[1-9])-", I would make the leading zero "greedy" optional, and capture it if it's there. It does not. And furthermore, it turns dates like "12-Mar-2011" into "2-Mar-2011". Obviously, I'd want matches in the teens, twenties, or thirties to get captured too.

what am I doing?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to reinvent the wheel. Why use regex to parse dates when you can use DateFormat/SimpleDateFormat?
 
Chris Treglio
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really trying to turn a String into a Date object, I'm trying to pull out String dates from a longer String filled with other stuff. My String sources are like "bla bla blah blah 02-Apr-2011 blah bla blah".

Can you do that with the DateFormat/SimpleDateFormat?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha. I'm not sure if that is possible.
 
Rancher
Posts: 4327
59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing the issue is that the initial .* is greedy, and that wins out over your other intentions here. It will probably overlook a leading 1, 2, or 3 as well, not just leading 0. As long as there's at least one digit after, to match the rest of the expression.

I suggest either:

(a) replace .* with .*?, which is reluctant

or

(b) drop the .* entirely, and replace matches() with find().
 
brevity is the soul of wit - shakepeare. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic