views:

51

answers:

3

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 should also work for "\r\n".

+2  A: 

Replace

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

with

\1

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

Tomalak
Doesn't that result in only single breaks?
Franky-D
@Franky: Not when I tested it. Does it for you?
Tomalak
+1  A: 
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.

steenslag
A: 

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"
Mikael S