Exercises, examples and other material relating to training module T247. This topic is presented on public courses Learning to program in Tcl
, Tcl Basics
Regular expressions are considerably enhanced in recent versions of Tcl, and this module, which assumes some prior knowledge of basic regular expressions, goes into advanced features such as groupings, sparse and greedy counts, and the Posix standard groups.
Related technical and longer articlesPattern Matching - a primer on regular Expressions
|Articles and tips on this subject||updated|
|4205||Regular Expression Substitution - Tcl|
regexp matches regular expressions in TCL, and it will return the latest match into a series of variables if you specify their names. regsub matches and replaces... not quite such a common requirement, and perhaps something I haven't blogged about in the past.
set newstory [regsub blue $original ...
|1412||Sparse and Greedy matching - Tcl 8.4|
Problem Analysis Once you have done a sparse or greedy count, all your following counts will also be sparse or greedy - this is a well documented bug in Tcl 8.4!
.* is a greedy match - any character and AS MANY AS POSSIBLE
.*? is a sparse match - any character but AS FEW AS POSSIBLE
|1613||Regular expression for 6 digits OR 25 digits|
I can write a regular expression to match ANY number of digits between 6 and 25 ...
but how would I write a regular expression to match either 6 digits or 25 digits, but no number in between?
(The regular expressions in this example are Tcl and ...
|1410||Tcl / regsub - changing a string and using interesting bits|
Regexp matches a string to a regular expression, and regsub goes one further in that it replaces the found string with something else, saving the transformed output into a new variable. But what if I want the output pattern to include part of the string that was matched? I can refer to the "interesting ...
|1336||Ignore case in Regular Expression|
Do you want to ignore case in a regular expression? There are a variety of ways of doing it ... depending on the language you're writing. Here are some hints:
/abcd/i Perl - an i after the regular expression
eregi PHP - use eregi rather than ereg
re.I or re.IGNORECASE Python - extra parameters ...
|1305||Regular expressions made easy - building from components|
There seems to be a certain macho desire in many programmer's minds to write a single complicated regular expression to match against an input line, ignorning the structured approach that everyone accepts quite cheerfully in almost every other case. Have a look at this Python line:
wholeline = r"\d\d-...-\d\d\d\d\s+(\d\d):(\d\d):(\d\d.\d\d),\s+(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+)"
|1195||Regular Express Primer|
Over the years I've been teaching people about Regular Expressions, I've learnt what does and doesn't work in such tuition. A casual question I saw yesterday got me writing, and I've just posted up a new technical article to the solution centre - see here (it will open in a separate window).
|943||Matching within multiline strings, and ignoring case in regular expressions|
Regular Expressions are powerful matching tools and you can specify almost anything within them. But there are certain facilities that are naturally applied to the regular expression as a whole rather than to parts of the match, and there are specified in a different way in each language / implementation.
Examples from our training material
|T247.tcl|| Tcl in use for CGI scripting|
|aretest|| advanced regular expression match demo|
|blob|| Progressive regsub examples|
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
What are advanced regular expressions?.
The working of a regular expression.
The elements of a regular expression.
A little more on regexp and regsub.
A further example.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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 700 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.