Global training solutions for engineers creating the world's electronics

Tcl Regular Expression Visualiser

NEW! version 1.3 available


Why do I need regular expressions?

Electronics engineers using HDLs often find that they need to process the contents of text files - HDL source code, report files generated by tools, netlists, and many others. Although many programmers instinctively expect to use Perl for text processing jobs, Tcl is often just as good - and it can save you the bother of learning a second scripting language!

When processing large amounts of text, regular expressions (REs) form a vital part of your toolkit. REs allow you to specify patterns in text, and Tcl can then search through your text (even megabytes of it!) to find places that match your RE. Once the RE has been matched, you can extract key items from the matched text using the "back reference" feature of regular expressions, and then perform substitutions on the text - a sophisticated and highly programmable version of the "search and replace" feature of most text editors.

REs are powerful and important, but unfortunately the syntax of REs is very terse and indigestible, so that many people find it daunting to learn and tricky to apply. For example, here's a RE that will search through a piece of text looking for a sentence that uses the same word more than once:

(^|\.)\W*(\m[^.]*\m((\w+)\M[^.]*\m\4M)[^.]*)(\.|\W*$)

That's an impressively powerful function for one line of regular expression, but it's not what I would call self-explanatory - which is why you need...

TREV, the Tcl Regular Expression Visualiser

To help delegates understand REs on our Tcl courses, we use a visualisation program written in Tcl/Tk. Recently we've enhanced that program so that it displays much more information about how the RE matched the original text, and we've given it an "Explore" feature that allows you to highlight parts of the original text that were identified by fragments of the RE. In this way you can quickly visualise the structure and the effect of even the most obscure RE, and try out its effect on various samples of source text.

Download TREV

  • trev13.zip source file or trev13.tar.Z source file
    This is the Tcl/Tk script. Run it under wish or any other environment that supports Tcl/Tk 8.3 or later. It has been tested in Windows and Solaris environments, and it should run correctly on any platform that supports Tcl/Tk.
     
  • RE library
    This is a simple text file containing a number of Tcl regular expressions that we have found useful. No doubt you will soon add plenty more of your own.