a.k.a. "Regular Expressions, part 2". How to match a string to a pattern, how to extract information from a match, how to repeat matches, to substitute one string of text for another, and how to translate on a character by character basis.
This topic is presented on public courses
Perl Programming,
Perl bootcamp
Examples from our training material
| bindemo | Printing out and reading in binary numbers |
| cats2 | Sample answer 2 |
| catshow | Sample answer 1 |
| favex.pl | Postcode, Zipcode, credit card no. etc - regular expression matches |
| filler | Using Regular Expressions to "mailmerge" |
| getlinks | Find all href links from a page |
| greedyvglobal.pl | Greedy matches v Global matches |
| html1 | Matching HTML - a greedy match doesn't work |
| html2 | Sparse matching, looking for an HTML tag |
| html3 | Global matching in a scalar context |
| html4 | Global matching in a list context |
| letters | Look for word starting and ending with same letter |
| n2 | Capturing groups into $1 and $2 |
| n3 | Capturing groups into a list |
| n4 | Special variables $' $& and $` |
| n5 | $PREMATCH, $MATCH and $POSTMATCH |
| name | Match and substitute (long winded way!) |
| name2 | Match and substitute - (example that fails) |
| name3 | Match and substitute executed block |
| names | Regular expression match - revision |
| ogado | Anagrams of First Great Western served stations |
| pcv1 | Postcode extractor - mark 1 |
| pcv2 | Postcode extractor Mk2 - save into named variables |
| pcv3 | Postcode, Mk3 - extract multiple postcodes |
| phone | Substitution using back reference |
| pwline | Character by character translation with tr |
| pwline2 | using tr to change multiple characters; also c and s switches |
| regextra | Splitting up a URL via a regex - sample exercise answer |
| rogues | Using tr to find invalid characters in a string |
| sedm | Substitue operator |
| totext | Converting < > and & to web standard sequences |
| yem | Perl regular expression - information returned |
| ystwyth.pl | binary data handling - examine a .gif file |
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Summary to date.
Extracting information from a match.
$1, $2, etc.
Assign to a list.
$`, $& and $Õ.
More about regular expressions.
What else can I put in regular expressions?.
More brackets.
Match modifiers.
Global v Greedy.
Alternative delimiters.
Some favourite regular expressions.
To match an email address.
To match a UK Postcode.
To match an American Zip code.
To match a date (UK Style).
To match a time.
To match a complete URL for a web page.
To match a Visa number.
To match a Mastercard number.
To match a UK Phone number.
To match a UK car registration plate.
To match a UK national insurance number.
To match a book's ISBN number.
Substitutions.
Substitute and execute.
Regular expression efficiency.
tr.
Handling binary text.
Summary.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 600 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.