Ruby or regex to strip excess line breaks -


How can you change it:

 Dear Fred How are you? Regards John 

In:

 Dear Fred How are you? Regards John 

Note: Single and double brakes are allowed, but not more than that. For example, we want to go here:

 "Dear Fred \ n \ n \ n \ nWhat are you? \ N \ n \ n \ n \ n \ n Responsibility \ nJohn" 

 "Dear Fred \ n \ nHow are you? \ N \ nQuestions \" John \ "

but also for" \ r \ n "

something like this?

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

At least works with your example:

  irb ( Main): 060: 0> S => "Dear Fred \ n \ n \ n \ nHow are you? \ N \ n \ n \ n \ n \ n \ nJohn "IRB (Main): 061: 0> S.gsub / (\ r? \ N) {3,} /, '\ 1 \ 1 '= & gt; "Dear Fred \ n \ nHow are you? \ N \ n Questions \ n John " 

Comments