How can I remove all words that have a length that is 3 or less?
"a lot of words in this text";
should become
"words this text"
Solution below. Note that your requirement ("remove all words...")
does not match the expected result, since you are also removing
whitespace around the words. That's why I added the second regex.
Still, the leading space is preserved. You can either add a third
regex to eliminate leading spaces, or you can split on ' '.
Keep in mind that if you split on ' ' you still won't have "words"
because punctuation will be included, for example. This is why I
would recommend against a split()/grep()/join() approach for this,
unless you are absolutely sure you don't need to worry about
punctuation or preserving spaces.
Ted
#!/usr/bin/perl
use warnings;
use strict;
my $text = "a lot of words in this text";
# note \w may not work well for you, adjust accordingly
$text =~ s/(\w+)/length $1 > 3 ? $1 : ''/eg;
# if you need multiple spaces collapsed to just one
$text =~ s/\s+/ /g;
print $text;