fork(1) download
  1. import re
  2. text = "This is some sample text beginning with a short paragraph.\n\nThis second paragraph is long enough to be split across lines, so it contains\na single newline in the middle.\n \nThis third paragraph has an unusual separator before it; a newline followed by\na space followed by another newline. It's a special case that needs to be\nhandled."
  3. print( re.sub(r'([^\S\n]*\n(?:[^\S\n]*\n)+[^\S\n]*)|[^\S\n]*\n[^\S\n]*', lambda x: x.group(1) or ' ', text) )
Success #stdin #stdout 0.02s 9460KB
stdin
Standard input is empty
stdout
This is some sample text beginning with a short paragraph.

This second paragraph is long enough to be split across lines, so it contains a single newline in the middle.
 
This third paragraph has an unusual separator before it; a newline followed by a space followed by another newline. It's a special case that needs to be handled.