This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Suggestions to exact variable length dates out of a string?

 
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following string:

Data Released September 11, 2006 at 10:30 A.M., For the Week Ending September 05, 2006

What I need to do is extract the two dates from the string.

A constant is the following:

Data Released [date 1] at [time], For the Week Ending [date 2]

I've been trying to use a combination of the String Object methods split(), indexof() and substring().

What is throwing me off is the variable length of the two dates.

I'd appreciate any suggestions or examples on how I can exact the two variable length dates out of a string.

Thanks.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the constant data IS constant, this might work

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do something with regular expressions (the java.util.regex package):

You can learn more about how regular expressions work from the Java Tutorial. They're very powerful, but contain some complexities that may require a bit of practice.
[ May 17, 2006: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a regular expression to parse the String:

[ May 17, 2006: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
You could do something with regular expressions (the java.util.regex package)...



That's what I was going to say!

Note though, you must call find(), matches(), or lookingAt() before querying the state of a Matcher or else an IllegalStateException will be thrown.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, yeah, thanks for catching that. I've edited my post above to fix the error.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much appreciated!

Thank you. Your suggestions are a lot better and faster than what I came up with:



Just shows, I have a lot to learn still...

Thanks again,
[ May 18, 2006: Message edited by: Todd Patrick ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic