J
Junkone
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
Junkone said:Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
A solution with unpack:
irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
=> "2007/08/01"
Hi --
And here's one with scanf:
=> "2007/08/01"
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![]()
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![]()
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,'/')
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regex
thanks
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().
using regexen for that's suicide unless you want to accept invalid
time strings.
Yes, but fast and incorrect is a bad combination.![]()
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.
You had to work on New Years' Eve? Bummer ...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/
Brian said:Yes, but fast and incorrect is a bad combination.![]()
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])
![]()
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.