Pages

Simple pattern matching

Perl is strong at text management, and regular expressions are a substantial part of its strenght. Here we start seeing some basic pattern matching.

Given a string:
my $text = "It was a dark and stormy night.";
A common task is checking if a substring is included in it.

The simplest version of it is the exact matching. To check if the substring "dark" is included in the original string, we can write:

if($text =~ /dark/) {
print "Yes, it's dark\n";
}

The matching operator is tilde (~), prefixed with an equal to check for actual matching, with a exclamation mark for the failure of the test, as shown here:

if($text !~ /light/) {
print "No, it's not light\n";
}

As usual in Perl, there is a way of avoiding some typing. If we put the string object of our search in the default scalar:
$_ = $text;
We could rewrite the pattern checking without explicitely refer to the original string. Besides, we could make our checking easier to change using a variable instead of a constant string:

my $pattern = "dark";
if(/$pattern/) {
print "Found it\n";
}

A usual requirement in pattern matching is relaxing it just a bit, to check the pattern in a case insensitive way.

The idea is that we usually want to know to have an OK when we check for "it" and we have "It" (or "IT", and even "iT"). The standard check it is too strict, in this case:

$pattern = "it";
if(not /$pattern/) {
print "Can't find it\n";
}

But we can easily overcome this issue using the "i" modifier:

if(/$pattern/i) {
print "Found it\n";
}

Chapter 5 of Beginning Perl by Simon Cozens focuses on regular expressions.

No comments:

Post a Comment