April 10, 2005

Python regular expressions

Regular expressions in python are for the most part pretty straight forward if you know how regular expressions work in general. I've used them quite often and had only a few little issues.

This time around I'm doing something different; loading a whole bunch of regular expressions from a file and building a list of compiled regex's for later use. The problem is that the string data either needs to be escaped to death or treated as a raw string. I tried several different approaches, none of which seemed to work and eventually settled on using the fragment below where I build a new raw string using substitution.

    lineData = parseLine( aLine )
    if lineData :
        regexData = r'%s' % lineData
        searchList.append( re.compile( regexData ))

It's probably not the most efficient way to do this (or perhaps it is) but I couldn't seem to find any examples and this works. Hopefully this will save someone else a couple hours flailing around in circles.

Posted by Dave at April 10, 2005 05:00 PM
Trackback URL: http://homie.dijas.com/mt/mt-tb.cgi/461
Comments