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

后端 未结 9 864
陌清茗
陌清茗 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:06

    are you sure it shouldn't be /\n\n\n/, "\n" that what you seem to be wanting in your question above.

    also, are you sure it's not doing a windows new-line "\r\n"?

    EDIT: Additional info

    Per Comment

    "The amount of newlines can change. Different lines have between 2 and 5 newlines."

    if you only want to hit the 2-5 lines try this

    /\n{2,5}/, "\n"
    

提交回复
热议问题