开发者

Ruby or regex to strip excess line breaks

开发者 https://www.devze.com 2023-01-18 02:20 出处:网络
How would you turn this: Dear Fred How are you? Regards John Into this: Dear Fred How are you? Regards John Note: Single and double breaks are allowed, but no more than that. For ex

How would you turn this:

Dear Fred



How are you?






Regards
John

Into this:

Dear Fred

How are you?

Regards
John

Note: Single and double breaks are allowed, but no more than that. For example, we want to go from:

"Dear Fred\n\n\n\nHow are you?\n\n\n\n\n\n\nRegards\nJohn"
to

"Dear Fred\n\nHow are you?\n\nRegards\nJohn"

But开发者_如何学C should also work for "\r\n".


Something like this?

s.gsub /(\r?\n){3,}/, '\1\1'

Seems to work with your example at least:

irb(main):060:0> s
=> "Dear Fred\n\n\n\nHow are you?\n\n\n\n\n\n\nRegards\nJohn"
irb(main):061:0> s.gsub /(\r?\n){3,}/, '\1\1'
=> "Dear Fred\n\nHow are you?\n\nRegards\nJohn"


Replace

(\r\n|\n|\r)\1+

with

\1

Where \1 refers to a back-reference. In ruby they are done through $1, I believe.


str.gsub!(/\n{3,}/, "\n\n")
str.gsub!(/(\r\n){3,}/, "\r\n\r\n")

The regex /\n{3,}/ searches for 3 or more consecutive linebreaks (\n). These are substituted with 2 linebreaks. Repeat for \r\n.

["\n", "\r\n"].each{|lb| str.gsub!( /(#{lb}){3,}/, lb*2 )}

Does the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消