lists and files question

Skip Montanaro skip at pobox.com
Wed Jul 23 23:44:32 CEST 2003


Jumping in late, and not meaning to step on John's toes:

    >> 2) The reason to use re.compile is for efficiency.  There is no need
    >> to call it inside the loop, since you're just recompiling the same
    >> regex over and over again.  Instead, compile the regex outside the
    >> loop
    >> 
    >> >>> rgx = re.compile('[A-Z]+')
    >> >>> for some_text in some_list:
    >> ...     m = rgx.match(some_text)

This usually doesn't buy you much because both regular expression modules
delivered with Python (sre via re, and pre if you choose it explicitly)
cache regular expression objects they compiles from strings.  Details are in
sre.py:_compile and pre.py:_cachecompile.

Skip





More information about the Python-list mailing list