any chance regular expressions are cached?
Ryan Ginstrom
software at ginstrom.com
Sun Mar 9 21:11:39 EDT 2008
> On Behalf Of Tim Chase
> Sounds like what you want is to use the compile() call to
> compile once, and then use the resulting objects:
>
> re1 = re.compile(r'\n')
> re2 = re.compile(r'^')
> ...
> s = re1.sub('\n' + spaces, s)
> s = re2.sub(spaces, s)
Yes. And I would go a step further and suggest that regular expressions are
best avoided in favor of simpler things when possible. That will make the
code easier to debug, and probably faster.
A couple of examples:
>>> text = """spam spam spam
spam spam
spam
spam"""
>>> # normalize newlines
>>> print "\n".join([line for line in text.splitlines() if line])
spam spam spam
spam spam
spam
spam
>>> # normalize whitespace
>>> print " ".join(text.split())
spam spam spam spam spam spam spam
>>> # strip leading/trailing space
>>> text = " spam "
>>> print text.lstrip()
spam
>>> print text.rstrip()
spam
>>> print text.strip()
spam
Regards,
Ryan Ginstrom
More information about the Python-list
mailing list