Pythonic way to trim and keep leading and trailing whitespace
Martin P. Hellwig
martin.hellwig at dcuktec.org
Tue Mar 23 19:57:23 EDT 2010
On 03/23/10 23:38, Tim Chase wrote:
<cut>
> Just in case you're okay with a regexp solution, you can use
>
> >>> s = "\t\tabc def "
> >>> import re
> >>> r = re.compile(r'^(\s*)(.*?)(\s*)$')
> >>> m = re.match(s)
> >>> m.groups()
> ('\t\t', 'abc def', ' ')
> >>> leading, text, trailing = m.groups()
<cut>
Ahhh regex, the hammer, Swiss Army Knife, cable tie, duct tape,
superglue and soldering iron, all wrapped in one*. Mastery of it will
enable you to drive the nails in your coffin at an incomparable speed. :-)
*Yes I was a sysadmin, why do you ask?
--
mph
More information about the Python-list
mailing list