11.13. Exercises: Regular expressions

Pick up a vocabulary. Based on the following template write a script that 
prints out every word from the list of words
(see examples/regex/dict.txt ) that

  • has an 'a'

  • starts with an 'a'

  • has 'th'

  • has an 'a' or an 'A'

  • has a '*' in it

  • starts with an 'a' or an 'A'

  • has both 'a' and 'e' in it

  • has an 'a' followed by an 'e' somewhere in it

  • does not have an 'a'

  • does not have an 'a' nor 'e'

  • has an 'a' but not 'e'

  • has at least 2 consequtive vowels (a,e,i,o,u)

  • has at least 3 vowels

  • has at least 6 characters

  • has at exactly 6 characters

  • Bonus: all the words with either 'aba' or 'ada' in them

  • Bonus: all the words with either 'aba' or 'eda' in them

  • Bonus: has a double character (e.g. 'oo')

  • Bonus: for every word print the first vowel

Example 11-2. examples/regex/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";
    }
}

If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed