[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regex help

Ezra Zygmuntowicz

6/10/2005 11:54:00 PM

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
ezra@yakima-herald.com



5 Answers

Nikolai Weibull

6/11/2005 12:22:00 AM

0

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

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Pete Elmore

6/11/2005 12:29:00 AM

0

Ezra Zygmuntowicz wrote:
> "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



Ezra Zygmuntowicz

6/11/2005 12:34:00 AM

0


On Jun 10, 2005, at 5:21 PM, Nikolai Weibull wrote:

> 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
> --
> Nikolai Weibull: now available free of charge at http:/...!
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
>

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com



Ezra Zygmuntowicz

6/11/2005 12:37:00 AM

0


On Jun 10, 2005, at 5:28 PM, Pete Elmore wrote:

> Ezra Zygmuntowicz wrote:
>
>> "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
>
>
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
ezra@yakima-herald.com



Nikolai Weibull

6/11/2005 12:40:00 AM

0

Pete Elmore wrote:

> Ezra Zygmuntowicz wrote:

> > "SPT_051205.jpg"

> 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

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}