Correction: Re: string substitutions
Mike Dean
klaatu at evertek.net
Sat Feb 23 17:41:53 EST 2002
More information about the Python-list mailing list
Sat Feb 23 17:41:53 EST 2002
- Previous message (by thread): Correction: Re: string substitutions
- Next message (by thread): Correction: Re: string substitutions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
* 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
- Previous message (by thread): Correction: Re: string substitutions
- Next message (by thread): Correction: Re: string substitutions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list