From: Brian Candler Date: 2003-03-16T23:51:09+09:00 Subject: Re: Can't think On Sun, Mar 16, 2003 at 07:33:59PM +0900, Tim Bates wrote: > I want to do this: > mystring.split("\n").delete_if{ |s| s =~ /^\s*$/ }.join("\n") > using gsub ('cause it'd be nicer). So what I'm doing is removing those lines > that consist of only whitespace. I tried this: > mystring.gsub(/^\s*$/, "") > This removes the whitespace, but not the newline, like the above one does. So > really, the question is how do I get the regexp to match the newline? mystring = "This is\n \nsome\n\n\ntext\n" p mystring.gsub(/^\s*\n/,"") >> "This is\nsome\ntext\n" i.e. since $ means 'match before newline' you can just a match a newline explicitly (and hence chomp it) However $ also means 'match end of string', so in this case a final line which consists only of spaces but with no trailing \n will not get removed. If that's a problem you can fix it by changing \n to (\n|$), although that gets a bit weird since ($|\n) behaves differently. Regards, Brian.