$str = "of course, japhy thought of this one"; $half = int(length($str)/2); $str =~ s {( ?)([^ ]*)(?<=^.{$half})([^ ]*)( ?)} { length($2) < length($3) ? "\n$2$3$4" : "$1$2$3\n" }e; print $str; __END__ of course, japhy thought of this one