Main Content

String matching in Perl with Regular Expressions

Archive - Originally posted on "The Horse's Mouth" - 2008-10-20 23:15:14 - Graham Ellis

Some languages used adaptive or overridden operator to perform a task depending on the operands - for example in Java, the "+" operator adds numbers, but concatenates strings, and in PHP the == operator compares numbers or strings, depending on the operand types. But in Perl 5, it's often up to the programmer to decide between a series of operators - the "+" always adds numbers and "." concatenates strings, with Perl converting the incoming data to the type needed for the operation, for example.

The need for careful choice of operator also applies for comparisons in Perl 5:
== != < <= > and >= compare numbers
eq ne lt le gt and ge compare text strings
=~ and !~ match a string against a pattern.

Matching a string against a pattern (known as a regular expression) is a very powerful feature indeed of Perl. Regular expressions originated in utilities such as grep, awk, sed and vi, and were then extended in egrep. With Perl, they have been extended even further - MUCH further - and they're at the heart of the language. It's said the imitation is the sincerest form of flattery, and you'll find Perl style regular expressions elsewhere too - in languages such as Java, Python and PHP.

Regular expressions contain many elements that you may use to match against a string, and in order to help newcomers to learn them, I divide the elements down into a series of groupings. There are literal characters that must match exactly - things like the @ of an email address. There are character groups - things like \d to match any digit. There are counts to tell the regular expression engine how many times the previous element occurs - things like {1,2} meaning once or twice. And there are anchors which let you specify whether you're looking at the beininning or the end of a string, or anywhere within it. Finally (for this intoduction) you can bracket parts of your regular expression to indicate that you want to capture parts of the incoming string that match THAT part of the expression.

It's much better explained in a table of element types, and with an example. And here's a program written to show Perl's regular expressions in use matching a British Postcode, which includes most of the elements:

=head1 Regular Expression Elements
   
* Anchors (or zero width assertions) - ^ $ (starts, end)
* Character groups - [A-Z], [0-9A-Z] [AEIOU] [^AEIOU]
   \d \s \w \D \S \W
   [[:digit:]] [^[:xdigit:]]
   .
* Literal characters - X x 3 \. \[ \? \
* counts - ? + * {2} {1,2}
* Interesting bits (or capture parentheses) - ()
   
=cut
   
while ($postcode = ) {
   
if ($postcode =~ /^\s*([A-Z]{1,2})\d[0-9A-Z]?\s+\d[A-Z]{2}\s*$/) {
   print "Yeah ... area ...\n";
} else {
   print "Nah ...\n";
}
}


Here's how that code runs:

Dorothy: grahamellis$ perl pcre
SN12 7NY
Yeah ... SN area ...
G12 6TH
Yeah ... G area ...
NPT1 6YH
Nah ...
Dorothy: grahamellis$


We run complete days on regular expressions and have many more details available on our web site. There's even a complete page on Perl Regular Expressions especially.