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"
···
--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]
A solution with unpack:
irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
=> "2007/08/01"
-- fxn
···
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
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
···
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
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, 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
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.
···
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
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama
Hi --
Hello
I have a date like 20070801 in a string. how do i change it to
2007/08/01 using regexA 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 Sun, 28 Oct 2007, Xavier Noria wrote:
On Oct 28, 2007, at 1:15 AM, Junkone wrote:
--
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!
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
thanksJust 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-forum.com/\.
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 28, 12:12 am, "ara.t.howard" <ara.t.how...@gmail.com> 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
> thanksusing regexen for that's suicide unless you want to accept invalid
time strings.
Yeah, but scanf is implemented in Ruby, so it's 25 times slower (at
least by a crude, quick benchmark) than unpack or insert
···
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"
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
···
On Oct 28, 2007, at 12:00 AM, Brian Adkins 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.
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama
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-forum.com/\.
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-forum.com/\.
ara.t.howard 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
a @ http://codeforpeople.com/
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama
You had to work on New Years' Eve? Bummer ...
···
On Oct 28, 2007, at 12:00 AM, Brian Adkins wrote:
Yes, but fast and incorrect is a bad combination.
···
On Oct 27, 11:21 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
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 insertThis 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().
hey! it worked for fortran!
···
On Oct 27, 2007, at 11:55 PM, Brian Adkins wrote:
Yes, but fast and incorrect is a bad combination.
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama
Brian Adkins wrote:
···
On Oct 27, 11:21 pm, 7stud -- <bbxx789_0...@yahoo.com> 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.
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])
--
Posted via http://www.ruby-forum.com/\.
7stud -- wrote:
Brian Adkins 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.
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
···
On Oct 27, 11:21 pm, 7stud -- <bbxx789_0...@yahoo.com> wrote:
--
Posted via http://www.ruby-forum.com/\.