7.2. ".", "[ ... ]"

In this slide we will learn how to specify any character or that a character will be one of a range of several possible characters.

The "." stands for any character

By putting a . character inside a regular expression, it means that it can match any character, excluding a newline. For example, the following snippet matches 5 letter words that start with 'l' and end with 'x':

use strict;
use warnings;

my $string = lc(shift(@ARGV));

if ($string =~ /l...x/)
{
    print "True\n";
}
else
{
    print "False\n";
}

The [ ... ] specifies more than one option for a character

When square brackets appear, one can specify more than one character inside them as option for matching. If the first character is ^ then they will match everything that is not one of the characters.

One can specify a range of characters with the hyphen. For example the pattern [a-zA-Z0-9_] matches every alpha-numeric character.

Here's an example that checks if a valid identifier for a perl variable is present in the string:

use strict;
use warnings;

my $string = lc(shift(@ARGV));

if ($string =~ /\$[A-Za-z_]/)
{
    print "True\n";
}
else
{
    print "False\n";
}

Written by Shlomi Fish