Exercises: Regular expressions


Pick up a file with some text in it. (e.g. examples/regex-perl/text.txt ). Write a script (one for each item) that prints out every line from the file that matches the requirement. You can use the script at the end of the page as a starting point but you will have to change it!


examples/regex-perl/regex_exercise.pl
#!/usr/bin/perl
use strict;
use warnings;

my $filename = shift or die "$0 FILENAME\n";
open my $fh, '<', $filename or die "Could not open '$filename'\n";

while (my $line = <$fh>) {
    if ($line =~ /REGEX1/) {
        print "has an a: $line";
    }

    if ($line =~ /REGEX2/) {
        print "starts with an a: $line";
    }
}