regular expresson for Unix and Dos Lineendings wanted

gene tani gene.tani at gmail.com
Thu Feb 23 09:12:21 EST 2006


Franz Steinhaeusler wrote:
> Hello, I need a regularexpression, which trims trailing whitespaces.
>
> While with unix line endings, it works;
> but not with Window (Dos) CRLF's:
>
> >>> import re
> >>> retrailingwhitespace = re.compile('(?<=\S)[ \t]+$', re.MULTILINE)
>
> 1) Windows
> >>> r="erewr    \r\nafjdskl     "
> >>> newtext, n = retrailingwhitespace.subn('', r)
> >>> n
> 1
> >>> newtext
> 'erewr    \r\nafjdskl'
>
> 2) Unix
> >>> r="erewr    \nafjdskl     "
> >>> newtext, n = retrailingwhitespace.subn('', r)
> >>> n
> 2
> >>> newtext
> 'erewr\nafjdskl'
> >>>
>
> Who can help me (regular expression, which works for both cases).

universal newlines:
http://www.python.org/doc/2.3.3/whatsnew/node7.html
http://mail.python.org/pipermail/python-list/2006-February/324410.html




More information about the Python-list mailing list