Wildcards in Regular Expressions: Learn Perl By Doing It, Part 7

You can use wildcards in your Perl regular expressions to match multiple characters. Wildcards enable you to match any character, or to match any of some subset of possible characters. In this tutorial we'll look at the dot wildcard, which can match any character. Later on we'll extend dot further by adding quantifiers that can specify how many characters to match.



 
use strict;
use warnings;

$|=1;

sub main {
    my $file = 'C:tutorialperlmymanjeeves.txt';
    
    open(INPUT, $file) or die("Input file $file not found.n");
    
    while(my $line = <INPUT>) {
        
        # The dot matches any character, even space or punctuation
        # e.g. the example below matches
        # I was
        # I said
        # If an
        # I take 
        # etc.
        # (only five characters including the space are actually matched by the epxression)
        if($line =~ /I..a./) {
            print $line;
        }
    }

    close(INPUT);
}

main();>