use strict;
use warnings;
# The ugly bit of text from your post
my $text = <
^[[2J^[[?7l^[[3;23r^[[?6l^[[24;27H^[[?25h^[[24;27H^[[?6l^[[1;24r^[[?7l^[[2J^[[24;27H^[[1;24r^[[24;27H^[[24;1HConnecting to Tacacs server^[[?25h^[[24;1H^[[24;1H ^[[?25h^[[24;1H^[[?25h^[[24;1H^[[24;1HUsername: ^[[?25h^[[24;1H^[[?25h^[[24;11H^[[24;11H^[[?25h^[[24;11H^[[1;1H^[[?25l^[[24;11H^[[24;1H ^[[?25h^[[24;11H
EOTXT # Remove all bits of text from an ESC character to the first uppercase alphabetic $text =~ s/\x1b[^A-Z]+[A-Z]//g; print "Remaining text is '$text'\n";