Convert string format

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

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”

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

On 10/28/07, Junkone [email protected] 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

Hi –

On Sun, 28 Oct 2007, Xavier N. 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”

David

On Oct 27, 7:10 pm, Junkone [email protected] 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,’/’)

On Oct 27, 10:09 pm, “David A. Black” [email protected] 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 :frowning:

Brian A. wrote:

On Oct 27, 10:09 pm, “David A. Black” [email protected] 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 :frowning:

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

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

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

Brian A. wrote:

On Oct 27, 7:10 pm, Junkone [email protected] 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.

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

On Oct 27, 11:21 pm, 7stud – [email protected] wrote:

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. :slight_smile:

On Oct 28, 12:12 am, “ara.t.howard” [email protected] wrote:

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.

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.

On Oct 27, 2007, at 11:55 PM, Brian A. wrote:

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

hey! it worked for fortran! :wink:

a @ http://codeforpeople.com/

ara.t.howard wrote:

that crashed for years every newyear’s eve for years until i got around

You had to work on New Years’ Eve? Bummer …

Brian A. wrote:

On Oct 27, 11:21 pm, 7stud – [email protected] wrote:

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. :slight_smile:

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])

:frowning:
:frowning:
:frowning:

7stud – wrote:

Brian A. wrote:

On Oct 27, 11:21 pm, 7stud – [email protected] wrote:

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. :slight_smile:

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])

:frowning:
:frowning:
:frowning:

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

On Oct 28, 2007, at 12:00 AM, Brian A. wrote:

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 :wink:

a @ http://codeforpeople.com/