in reply to Stripping out specific numbers

One way:

>perl -wMstrict -le "my $s = 'xx123xx4321xx56789xx987654xx'; ;; my @n = $s =~ m{ (?<! \d) \d{4,5} (?! \d) }xmsg; printf qq{'$_' } for @n; " '4321' '56789'