# $start and $window $i++ while($i<$start && /(\S+\s+)/g); $i=0; while(/\G(\S+\s+)/g && $i<$window_length) { $newtext .= "$1"; $i++; }