[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

convert string format

Junkone

10/27/2007 11:10:00 PM

Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks

17 Answers

Tim Hunter

10/27/2007 11:28:00 PM

0

Junkone wrote:
> Hello
> I have a date like 20070801 in a string. how do i change it to
> 2007/08/01 using regex
> thanks
>
>

irb(main):001:0> x = '20070801'
=> "20070801"
irb(main):005:0> d = /(\d{4})(\d{2})(\d{2})/.match x
=> #<MatchData:0x56520c>
irb(main):006:0> d[1..3]
=> ["2007", "08", "01"]
irb(main):007:0> d[1..3].join("/")
=> "2007/08/01"

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]

Xavier Noria

10/27/2007 11:30:00 PM

0

On Oct 28, 2007, at 1:15 AM, Junkone wrote:

> Hello
> I have a date like 20070801 in a string. how do i change it to
> 2007/08/01 using regex

A solution with unpack:

irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
=> "2007/08/01"

-- fxn


Tom Machinski

10/27/2007 11:31:00 PM

0

On 10/28/07, Junkone <junkone1@gmail.com> wrote:
> Hello
> I have a date like 20070801 in a string. how do i change it to
> 2007/08/01 using regex
> thanks

irb(main):008:0> '20070801'.sub( /(\d{4})(\d{2})(\d{2})/ ) { [ $1, $2,
$3 ].join( '/' ) }
=> "2007/08/01"

OR:

irb(main):010:0> '20070801'.sub( /(\d{4})(\d{2})(\d{2})/, '\1/\2/\3')
=> "2007/08/01"

-Tom

Brian Adkins

10/28/2007 1:25:00 AM

0

On Oct 27, 7:10 pm, Junkone <junko...@gmail.com> wrote:
> Hello
> I have a date like 20070801 in a string. how do i change it to
> 2007/08/01 using regex
> thanks

Just out of curiosity, why do you want to use regular expressions to
solve this? Don't you just want to insert two '/' characters in the
appropriate place?

"20070801".insert(4,'/').insert(7,'/')

David A. Black

10/28/2007 2:09:00 AM

0

Brian Adkins

10/28/2007 2:44:00 AM

0

On Oct 27, 10:09 pm, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
> On Sun, 28 Oct 2007, Xavier Noria wrote:
> > On Oct 28, 2007, at 1:15 AM, Junkone wrote:
>
> >> Hello
> >> I have a date like 20070801 in a string. how do i change it to
> >> 2007/08/01 using regex
>
> > A solution with unpack:
>
> > irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
> > => "2007/08/01"
>
> And here's one with scanf:
>
> >> "20070801".scanf("%4s%2s%2s").join('/')
> => "2007/08/01"

Yeah, but scanf is implemented in Ruby, so it's 25 times slower (at
least by a crude, quick benchmark) than unpack or insert :(

7stud --

10/28/2007 2:55:00 AM

0

Brian Adkins wrote:
> On Oct 27, 10:09 pm, "David A. Black" <dbl...@rubypal.com> wrote:
>>
>> > irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
>> > => "2007/08/01"
>>
>> And here's one with scanf:
>>
>> >> "20070801".scanf("%4s%2s%2s").join('/')
>> => "2007/08/01"
>
> Yeah, but scanf is implemented in Ruby, so it's 25 times slower (at
> least by a crude, quick benchmark) than unpack or insert :(

I was going to post that same solution, but after timing it, I almost
gagged when I saw the results.

--
Posted via http://www.ruby-....

7stud --

10/28/2007 3:22:00 AM

0

Brian Adkins wrote:
>
> Yeah, but scanf is implemented in Ruby, so it's 25 times slower (at
> least by a crude, quick benchmark) than unpack or insert :(
>

This one is super speedy:

str = '20070801'
new_str = sprintf("%4s/%2s/%2s", str, str, str)


On my system sprintf() is twice as fast as unpack(), which is twice as
fast as match().


--
Posted via http://www.ruby-....

7stud --

10/28/2007 3:30:00 AM

0

Brian Adkins wrote:
> On Oct 27, 7:10 pm, Junkone <junko...@gmail.com> wrote:
>> Hello
>> I have a date like 20070801 in a string. how do i change it to
>> 2007/08/01 using regex
>> thanks
>
> Just out of curiosity, why do you want to use regular expressions to
> solve this? Don't you just want to insert two '/' characters in the
> appropriate place?
>
> "20070801".insert(4,'/').insert(7,'/')
>

Ack. I forgot to time that one....and we have a winner. Nice.

--
Posted via http://www.ruby-....

ara.t.howard

10/28/2007 4:13:00 AM

0


On Oct 27, 2007, at 5:15 PM, Junkone wrote:

> Hello
> I have a date like 20070801 in a string. how do i change it to
> 2007/08/01 using regex
> thanks
>
>

using regexen for that's suicide unless you want to accept invalid
time strings. use the functionality already provided by ruby:

cfp:~ > cat a.rb
require 'time'

puts Time.parse('20070801').strftime('%Y/%m/%d')

puts Time.parse('20070842').strftime('%Y/%m/%d') rescue puts $!.message


cfp:~ > ruby a.rb
2007/08/01
argument out of range


kind regards.


a @ http://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama