> -----Original Message----- > From: Alex Young [mailto:alex / blackkettle.org] > Sent: Tuesday, June 27, 2006 2:32 PM > To: ruby-talk ML > Subject: Re: string manipulation > > siva kumar wrote: > > How to add "-" between the numbers > > Eg: > > 123456789123456789 as 12-3456-7891-2345-6789 > > > > > irb(main):001:0> str = "123456789123456789" > => "123456789123456789" > irb(main):002:0> str.reverse.gsub(/..../, "\\0-").reverse > => "12-3456-7891-2345-6789" > > -- > Alex That's awesome! But doesn't guarantee 2 digits at the start (who knows what the OP needs). I was messing with: str.scan(/(..)(.*)/).first.map {|e| e.length>4?e.scan(/.{1,4}/):e}.join('-') which is awful, then I got: str.sub(/\d{2}/,'\0-').gsub(/\d{4}(?!$)/,'\0-') Which is... probably even worse. :P ben