Sagewire Logo

convert string format

17 Message(s) by 9 Author(s) originally posted in ruby programming


From: Junkone Date:   Saturday, October 27, 2007
Hello
I have a date like 20070801 in a string. how do I change it to
2007/08/01 using regex
thanks


From: Tim Hunter Date:   Saturday, October 27, 2007
wrote in message:
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]


From: Xavier Noria Date:   Saturday, October 27, 2007
wrote in message:

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


From: Tom Machinski Date:   Saturday, October 27, 2007
wrote in message:
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


From: Brian Adkins Date:   Saturday, October 27, 2007
wrote in message:
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? Do not you just want to insert two '/' characters in the
appropriate place?

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


From: Brian Adkins Date:   Saturday, October 27, 2007
wrote in message:
Hi --
wrote in message:
wrote in message:
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 :(


From: 7stud -- Date:   Saturday, October 27, 2007
wrote in message:
wrote in message:

> 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/.


From: 7stud -- Date:   Saturday, October 27, 2007
wrote in message:
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/.


From: 7stud -- Date:   Saturday, October 27, 2007
wrote in message:
wrote in message:
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? Do not 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've a winner. Nice.

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


From: ara.t.howard Date:   Sunday, October 28, 2007
wrote in message:

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 $!.messagecfp:~ > ruby a.rb
2007/08/01
argument out of rangekind regards.a @xxxxxxxxxxx http://codeforpeople.com/
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama


From: Brian Adkins Date:   Sunday, October 28, 2007
wrote in message:
wrote in message:
> 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().



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


From: Brian Adkins Date:   Sunday, October 28, 2007
wrote in message:
wrote in message:
> 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 isn't user input, but serialized data that's already been parsed/
validated and just needs to be formatted.


From: ara.t.howard Date:   Sunday, October 28, 2007
wrote in message:

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



hey! it worked for fortran! ;-)

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


From: ara.t.howard Date:   Sunday, October 28, 2007
wrote in message:

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



could be. still, c programs aren'torious 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 @xxxxxxxxxxx http://codeforpeople.com/
--
it isn't enough to be compassionate. you must act.
h.h. the 14th dalai lama


From: M. Edward Date:   Sunday, October 28, 2007
wrote in message:
wrote in message:
Good point, but with a format like "20070801", there's a good chance
this isn't user input, but serialized data that's already been parsed/
validated and just needs to be formatted.
could be. still, c programs aren'torious 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 @xxxxxxxxxxx http://codeforpeople.com/
--
it isn't enough to be compassionate. you must act.
h.h. the 14th dalai lama


You had to work on New Years' Eve? Bummer ...


From: 7stud -- Date:   Sunday, October 28, 2007
wrote in message:
wrote in message:
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/.


From: Stefan Rusterholz Date:   Sunday, October 28, 2007
wrote in message:
wrote in message:
wrote in message:
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
--
Posted via http://www.ruby-forum.com/.



Next Message: A memcached-like server in Ruby - feasible?


Blogs related to convert string format

24 new messages in 15 topics - digest
and then see the difference between this today and the string that I > converted to date. > now in the first place I can't recall how I can convert a string to a date. > then now I don't know how to calculate difference in days between ...

25 new messages in 10 topics - digest
So, I'm trying to figure out how to convert this binary field I get back from Active Directory, into a hexidecimal field. I think I need to use the unpack method, but I haven't figured out what format string to pass the method yet. ...

25 new messages in 15 topics - digest
Convert string to command.. - 1 messages, 1 author http://groups.google.com/group/comp.lang.python/browse_thread/thread/4dc5489d3687fb0a?hl=en * Enso or Launchy like UI from Python - 1 messages, 1 author ...

25 new messages in 15 topics - digest
convert nil into String (TypeError) from reads_controller.rb: in > "initialize', from reads_controller.rb: in 'new', from > reads_controller.rb > > Please help me. I'ma newbie for Ruby :(. The file you show is probably part of a Rails ...

25 new messages in 14 topics - digest
{4}/, :message => " must be in the format dd/mm/yyyy") > > Now, when I use the eclipse plugin QuickRex to get my regular > expression, the string value "02/02/1997" matches my pattern. Do > ruby regular expression not accept "\d" as a ...

25 new messages in 16 topics - digest
format string for array question - 1 messages, 1 author http://groups.google.com/group/ruby-talk-google/browse_thread/thread/16e43aa5ddfb0803?hl=en * Conflicts between using respond_to? and extending Ruby - 2 messages, 1 ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional