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 ... $1 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.
(written 2008-10-20, updated 2008-11-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q802 - Object Orientation and General technical topics - Regular Expression Elements [453] Commenting Perl regular expressions - (2005-09-30)
[1480] Next course - 7th January 2008, Regular Expressions - (2007-12-21)
[1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
[1799] Regular Expressions in PHP - (2008-09-16)
[2804] Regular Expression Myths - (2010-06-13)
[4505] Regular Expressions for the petrified - in Ruby - (2015-06-03)
[4763] Regex Reference sheet - (2017-10-10)
P205 - Perl - Initial String Handling [31] Here documents - (2004-08-28)
[254] x operator in Perl - (2005-03-22)
[324] The backtick operator in Python and Perl - (2005-05-25)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[987] Ruby v Perl - interpollating variables - (2006-12-15)
[1195] Regular Express Primer - (2007-05-20)
[1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[2798] Perl - skip the classics and use regular expressions - (2010-06-08)
[2816] Intelligent Matching in Perl - (2010-06-18)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2963] Removing the new line with chop or chomp in Perl - what is the difference? - (2010-09-21)
[3005] Lots of ways of doing it in Perl - printing out answers - (2010-10-19)
[3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
[3547] Using Perl to generate multiple reports from a HUGE file, efficiently - (2011-12-09)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3770] Sample answers to training course exercises - available on our web site - (2012-06-21)
Some other Articles
Well structured coding in PerlPerl and BlackberriesPictures from a delegateDaisy the Cow and a Pint of Ginger BeerString matching in Perl with Regular Expressions30th November - Santa Trip from MelkshamLua - IAQ (Infrequently Answered Questions)Old Piles of the South WestPassing a table from Lua into CCalling functions in C from your Lua script - a first HowTo