I have a script written in ruby. I need to remove any duplicate newlines (e.g.)
\\n \\n \\n
to
\\n
My current
You need to match more than one newline up to an infinite amount. Your code example will work with just a minor tweak:
str.gsub!(/\n+/, "\n")
For example:
str = "this\n\n\nis\n\n\n\n\na\ntest" str.gsub!(/\n+/, "\n") # => "this\nis\na\ntest"