I’m too tired to think this out for myself, and I can’t find it in the
pickaxe, so I’m going to be lazy and bug the list; no doubt someone will see
the solution right away.
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?
On Sun, 16 Mar 2003 19:33:59 +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?
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.
···
On Sun, Mar 16, 2003 at 07:33:59PM +0900, Tim Bates wrote: