RE strings (was: Variable Interpolation - status of PEP 215)

Tim Peters tim.one at comcast.net
Fri Jun 21 19:42:54 EDT 2002


[Cimarron Taylor]
> ...
>    import re
>    emp_re = re.compile(r'^EMP:([^,]*),([^,]*),([^,]*),([^,]*)$')
>
>    def parseline2(line):
>       m = emp_re.match(line)
>       if m:
>           ...
> ...
>
>    import re
>    def parseline3(line):
>       m = re.match(r'^EMP:([^,]*),([^,]*),([^,]*),([^,]*)$', line)
>       if m:
>           ...
>
> Unless I'm mistaken about how the re module works, parseline3 will
> recompile the regex each time it is called.

It does not.  re maintains internal caches of compiled regexp objects, and
has done so in all versions of Python to date.

There's a still a good reason to compile your regexps at module level,
though:  then you've got lots of room to use re.VERBOSE mode and add useful
comments to these dreadful beasts <0.7 wink>.






More information about the Python-list mailing list