Main Content

Keeping your regular expressions simple

Archive - Originally posted on "The Horse's Mouth" - 2006-04-05 12:15:18 - Graham Ellis

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!