Well, your regex is going to fail to file words at the beginning and end of lines. That is what the \b
assertion is for:
#!/use/bin/perl
use strict;
use warnings;
use Text::Wrap;
my $file = "alice.txt";
open my $fh, "<", $file
or die "could not open $file: $!";
my @words;
while (<$fh>) {
push @words, /\b(t\we)\b/g;
}
print "three letter words that start with t and end with e:\n",
wrap "\t", "\t", "@words\n";
You can find four letter words by just looking for anything that is a word character that has more than 3 characters. The \w
character class matches word characters and the quantifer {4,} say to match the previous pattern 4 or more times. Put them together with the word boundary assertion and you get /\b\S{4,}\b/
:
#!/use/bin/perl
use strict;
use warnings;
use Text::Wrap;
my $file = "alice.txt";
open my $fh, "<", $file
or die "could not open $file: $!";
my @three;
my @four;
while (<$fh>) {
push @three, /\b(t\we)\b/g;
push @four, /\b(\w{4,})\b/g;
}
print "three letter words that start with t and end with e:\n",
wrap("\t", "\t", "@three\n"),
"four letter words:\n",
wrap "\t", "\t", "@four\n";
You may want to use [[:alpha:]]
instead of \w
if you don't want to match things like "t0e"
.