How to replace multiple newlines in a row with one newline using Ruby

后端 未结 9 967
陌清茗
陌清茗 2021-02-05 03:42

I have a script written in ruby. I need to remove any duplicate newlines (e.g.)

\\n
\\n
\\n

to

\\n

My current

9条回答
  •  长发绾君心
    2021-02-05 04:04

    Additionally, also works with

    • spaces on blank lines
    • n number of back to back blank lines

    str.gsub! /\n^\s*\n/, "\n\n"

    where,

    • \n is of course newline
    • \s is space
    • denotes 1 or more spaces along when used after \s

提交回复
热议问题