s/ ^ # Start at the beginning. ( # Start capturing. [^\s]* # 0 or more non-space. \s [^\s]* # A space and 0 or more non-spaces... once. \s [^\s]* # twice. \s [^\s]* # three times. ) # Stop capturing. \s # Match another space. /$1\n/x # Replace with what we captured and a newline.