From: Brian Candler Date: 2010-10-04T18:17:12+09:00 Subject: Re: save only first line from string? John Sikora wrote: > Terry Michaels wrote: >> Hi. What's the most simple and elegant way to remove all the contents of >> a String except for the first line? (Assume string consist of one line, >> multiple lines, or no lines, and assume that we don't know which OS we >> are on.) > > I don't know about the most elegant or simple way, but how about this: > > string.sub!(/^(.*\n?)((?im).+)?/, '\1').chomp Why not just this? string.sub!(/\n.*/m, '') Or if you are a Windows user, string.sub!(/[\r\n].*/m, '') -- Posted via http://www.ruby-forum.com/.