convert string format

J

Junkone

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

Tim Hunter

Junkone said:
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"
 
X

Xavier Noria

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
 
T

Tom Machinski

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
 
B

Brian Adkins

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,'/')
 
D

David A. Black

Hi --

A solution with unpack:

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

And here's one with scanf:
=> "2007/08/01"


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
B

Brian Adkins

Hi --





And here's one with scanf:

=> "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 :(
 
7

7stud --

Brian said:
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.
 
7

7stud --

Brian said:
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().
 
7

7stud --

Brian said:
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.
 
A

ara.t.howard

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://codeforpeople.com/
 
B

Brian Adkins

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().

Yes, but fast and incorrect is a bad combination. :)
 
B

Brian Adkins

using regexen for that's suicide unless you want to accept invalid
time strings.

Good point, but with a format like "20070801", there's a good chance
this is not user input, but serialized data that's already been parsed/
validated and just needs to be formatted.
 
A

ara.t.howard

Good point, but with a format like "20070801", there's a good chance
this is not user input, but serialized data that's already been
parsed/
validated and just needs to be formatted.

could be. still, c programs are notorious for using crappy methods
of string/date generation and dumping out bad dates - we had one
system that crashed for years every newyear's eve for years until i
got around to hacking a wrapper on it... give you one guess why ;-)

a @ http://codeforpeople.com/
 
M

M. Edward (Ed) Borasky

ara.t.howard said:
could be. still, c programs are notorious for using crappy methods of
string/date generation and dumping out bad dates - we had one system
that crashed for years every newyear's eve for years until i got around
to hacking a wrapper on it... give you one guess why ;-)

a @ http://codeforpeople.com/
You had to work on New Years' Eve? Bummer ...
 
7

7stud --

Brian said:
Yes, but fast and incorrect is a bad combination. :)

No wonder it was so much faster than this one:

str = '20070801'
new_str = sprintf("%s/%s/%s", str[0..3], str[4..5], str[6..7])


:(
:(
:(
 
S

Stefan Rusterholz

7stud said:
Brian said:
Yes, but fast and incorrect is a bad combination. :)

No wonder it was so much faster than this one:

str = '20070801'
new_str = sprintf("%s/%s/%s", str[0..3], str[4..5], str[6..7])


:(
:(
:(

Well, use sprintf("%s/%s/%s", str[0,4], str[4,2], str[6,2]) then, it's
still a lot faster than the other solutions.

Regards
Stefan
 

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

Members online

Forum statistics

Threads
474,269
Messages
2,571,338
Members
48,028
Latest member
chasetony

Latest Threads

Top