Regex help

E

Ezra Zygmuntowicz

Hey there list
I hope you geniuses can help me with a little regex and date magic.
Say that I have a string like this:

"SPT_051205.jpg"

How could I strip out the numbers that stand for the date and convert
it to look like this:

"May 12 2005"

Any suggestions are much appreciated!
Thanks-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 
N

Nikolai Weibull

Ezra said:
Say that I have a string like this:

"SPT_051205.jpg"

How could I strip out the numbers that stand for the date and convert
it to look like this:

"May 12 2005"

"SPT_051205.jpg".scan(/(\d\d)(\d\d)(\d\d)/) do |a|
puts Time.mktime(a[2].to_i + 2000, a[0].to_i, a[1].to_i).strftime("%b %d %Y")
end

Enjoy,
nikolai
 
P

Pete Elmore

Ezra said:
"SPT_051205.jpg"

How could I strip out the numbers that stand for the date and convert
it to look like this:

"May 12 2005"

#!/usr/bin/env ruby
require 'date'
def date_from_special_string(s)
/(\d\d)(\d\d)(\d\d)/.match s
month = $1
day = $2
year = "20#{$3}"
Date::parse("#{month}/#{day}/#{year}").strftime("%B %d %G")
end

puts date_from_special_string "SPT_051205.jpg"

There was a little ambiguity in the example (Was the month the first 05,
or was that the year?), so you may need to swap the $1 and $3.

Hope that was helpful.

Pete
 
E

Ezra Zygmuntowicz

Ezra Zygmuntowicz wrote:

Say that I have a string like this:

"SPT_051205.jpg"

How could I strip out the numbers that stand for the date and convert
it to look like this:

"May 12 2005"

"SPT_051205.jpg".scan(/(\d\d)(\d\d)(\d\d)/) do |a|
puts Time.mktime(a[2].to_i + 2000, a[0].to_i, a[1].to_i).strftime
("%b %d %Y")
end

Enjoy,
nikolai

Thanks so much Nikolai it works perfect!
-Ezra
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 
E

Ezra Zygmuntowicz

#!/usr/bin/env ruby
require 'date'
def date_from_special_string(s)
/(\d\d)(\d\d)(\d\d)/.match s
month = $1
day = $2
year = "20#{$3}"
Date::parse("#{month}/#{day}/#{year}").strftime("%B %d %G")
end

puts date_from_special_string "SPT_051205.jpg"

There was a little ambiguity in the example (Was the month the
first 05, or was that the year?), so you may need to swap the $1
and $3.

Hope that was helpful.

Pete
Thanks Pete, that helps a lot as well. I guess I should have picked a
better example too. But you got it right the first 05 is the month
and the last one was the year. I'll take care to use an unambiguous
sample next time.

Thanks!

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 
N

Nikolai Weibull

Pete said:
Ezra Zygmuntowicz wrote:

Date::parse("#{month}/#{day}/#{year}").strftime("%B %d %G")

Are you sure %G is what you want here? I’m betting that the dates
generated for the names of these JPEGs aren’t conforming to ISO 8601 and
timezone settings. Or perhaps I’m misunderstanding the usefulness of
%G. Also, Ezra, the use of the fifth month (May) was ambiguous, as it’s
unclear whether you want the full month name (%B) or its three-letter
abbreviation (%b).
There was a little ambiguity in the example (Was the month the first
05, or was that the year?), so you may need to swap the $1 and $3.

The <year><day><month> template is definitely one of the weirder ones,
although <month><day><year> is certainly a bit weird as well :),
nikolai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Double posts? 7
Regex Help 4
Regex help 6
RubyConf 05 Audio and Video files resurrected! 1
RubyCocoa question 5
Re RubyConf Presentation Audio 2
Qt 4.0 ruby bindings? 3
rubyforge down? 3

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top