...

/

Solution Review: Extract Email Addresses

Solution Review: Extract Email Addresses

See the solution to the exercise on finding email addresses from the text of a file.

We'll cover the following...

Solution

Let’s look at the solution before jumping into the explanation:

Press + to interact
main.pl
Emails1.txt
Emails2.txt
Emails3.txt
# Assume that $file is already defined and it contains the name of the file
open my $fh, '<', $file;
my $username = qr/\w+/;
my $domain = qr/\w+/;
my $top_level = qr/\w{2,3}/;
my $country = qr/[a-z]{2,3}/;
my $email = qr/$username\@$domain\.$top_level(\.$country)?/;
while(<$fh>) {
say "$1" while $_ =~ /($email)/ig;
}

Note: You can try out ...