There's something macho about programmers when it comes to regular expressions - so often they'll try and do the whole job in a single regular expression when doing a two stage process is much more logical, much quicker in operation, and far easier to code.
Take a requirement that came up this morning, for example, to extract data from a netlist. The data to be handled might look like:
[5560] = !V5*V6*V7*!V8 + [5223]
and the requirement is to return a list elements separated by = or + signs, and then to return a list of nodes or inputs within those elements. In other words, what's wanted is:
['5560']
['!V5', 'V6', 'V7', '!V8']
['5223']
I think I have rather made my point already by how I described the requirement, haven't I? Two regular expressions ...
I'm running a
Python course, so the example's in Python ...
full source code and
more Python regular expression examples available!
(written 2006-04-05 12:15:18)
Associated topics are indexed under
Q806 - Regular Expression Cookbook [2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
[2563] Efficient debugging of regular expressions - (2010-01-04)
[2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1230] Commenting a Perl Regular Expression - (2007-06-12)
Y115 - Additional Python Facilities [2655] Python - what is going on around me? - (2010-02-28)
[2462] Python - how it saves on compile time - (2009-10-20)
[2435] Serialization - storing and reloading objects - (2009-10-04)
[2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
[1876] Python Regular Expressions - (2008-11-08)
[1337] A series of tyre damages - (2007-09-08)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1149] Turning objects into something you can store - Pickling (Python) - (2007-04-15)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1043] Sending an email from Python - (2007-01-18)
[901] Python - listing out the contents of all variables - (2006-10-21)
[753] Python 3000 - the next generation - (2006-06-09)
[663] Python to MySQL - (2006-03-31)
[463] Splitting the difference - (2005-10-13)
[239] What and why for the epoch - (2005-03-08)
[208] Examples - Gadfly, NI Number, and Tcl to C interface - (2005-02-10)
[183] The elegance of Python - (2005-01-19)
Some other Articles
Melksham, WiltshireAdding PHP tags to an old cgi programWiltshire to get an extra MPHelicopter views and tartansKeeping your regular expressions simpleBoth ends of the animalArchitectural Heritage - DevizesThe best Open Source library in townPython - block insets help with documentationWell House Manor goes ahead