Correction: Re: string substitutions

Mike Dean klaatu at evertek.net
Sat Feb 23 17:41:53 EST 2002


* Mike Dean <klaatu at evertek.net> [2002-23-02 14:22]:
> import re
> 
> # Replace all occurances of one or more spaces with a single space
> re.sub(' +', ' ', mystring)
> # Ditto for newlines
> re.sub('\n+', '\n', mystring)
> # 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)...
the following (using a lambda to return the first character of the
string of whitespace) works:

re.sub('(\n+| +)', (lambda m:m.group(0)[0]), mystring)

If there's a way (there probably is) to write this without the lambda
trickery, could someone enlighten me?

will-try-to-remember-to-proofread-code-before-posting-ly y'rs
Mike




More information about the Python-list mailing list