Correction: Re: string substitutions
sjmachin at lexicon.net
Sun Feb 24 07:28:50 CET 2002
Mike Dean <klaatu at evertek.net> wrote in message news:<mailman.1014504639.21135.python-list at python.org>...
> * Mike Dean <klaatu at evertek.net> [2002-23-02 14:22]:
> > import re
> > # And, to combine the two into one operation:
> > re.sub('(\n+| +)', '\1', mystring)
> This last line doesn't work as intended - sorry! (didn't proofread it
> carefully enough :-()
> It won't convert those chars to single characters
> (in fact, it does absolutely nothing except consume processor time)...
Try testing instead of proofreading. Post actual examples of input and
output, like this:
>>> mystring = "aaaa\n\n\n\nbbbbbbb\n\n\nbb\nbbbb"
>>> re.sub('(\n+| +)', '\1', mystring)
>>> re.sub('(\n+| +)', r'\1', mystring)
Your posted code replaces matched substrings with "\x01".
What you *meant* to post does nothing but chew up CPU time.
More information about the Python-list