$window_start = 5; $window_size = 5; $text_body = "Please ignore all this text, this is the text wanted, please none of this text"; if ($window_start > 0) { $text_body =~ /\S+/g foreach 1..$window_start; $start_index = pos($text_body); } else { $start_index = 0; } $text_body =~ /\S+/g foreach 1..$window_size; $end_index = pos($text_body); $windowed_text = substr($text_body, $start_index, ($end_index - $start_index)); print $windowed_text; #prints " this is the text wanted,"