#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11135746 use warnings; use Data::Dump 'dd'; local $_ = 'text three four K lastword'; for my $len ( 30 .. length ) { my $text = s/^.{0,$len}\b\K.*//sr =~ s/<[^<>]*\z//sr . '...'; dd $text; } #### "text three ..." "text three four..." "text three four..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four ..." "text three four K..." "text three four K..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K ..." "text three four K lastword..."