Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
String matching in Perl with Regular Expressions

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 23:15:14)

 
Associated topics are indexed under
P205 - Perl - Initial String Handling
Q802 - Object Orientation and General technical topics - Regular Expression Elements

Back to
30th November - Santa Trip from Melksham
Previous and next
or
Horse's mouth home
Forward to
Daisy the Cow and a Pint of Ginger Beer
Some other Articles
Well structured coding in Perl
Perl and Blackberries
Pictures from a delegate
Daisy the Cow and a Pint of Ginger Beer
String matching in Perl with Regular Expressions
30th November - Santa Trip from Melksham
Lua - IAQ (Infrequently Answered Questions)
Old Piles of the South West
Passing a table from Lua into C
Calling functions in C from your Lua script - a first HowTo
1975 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho