Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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, updated 2008-11-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P205 - Perl - Initial String Handling
  [3770] Sample answers to training course exercises - available on our web site - (2012-06-21)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3547] Using Perl to generate multiple reports from a HUGE file, efficiently - (2011-12-09)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3005] Lots of ways of doing it in Perl - printing out answers - (2010-10-19)
  [2963] Removing the new line with chop or chomp in Perl - what is the difference? - (2010-09-21)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [2798] Perl - skip the classics and use regular expressions - (2010-06-08)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
  [1195] Regular Express Primer - (2007-05-20)
  [987] Ruby v Perl - interpollating variables - (2006-12-15)
  [970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
  [324] The backtick operator in Python and Perl - (2005-05-25)
  [254] x operator in Perl - (2005-03-22)
  [31] Here documents - (2004-08-28)

Q802 - Object Orientation and General technical topics - Regular Expression Elements
  [4763] Regex Reference sheet - (2017-10-10)
  [4505] Regular Expressions for the petrified - in Ruby - (2015-06-03)
  [2804] Regular Expression Myths - (2010-06-13)
  [1799] Regular Expressions in PHP - (2008-09-16)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [1480] Next course - 7th January 2008, Regular Expressions - (2007-12-21)
  [453] Commenting Perl regular expressions - (2005-09-30)


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
4759 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, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1849_Str ... sions.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat