Regex to match all trailing whitespace _and_ newlines.

Dotan Cohen dotancohen at gmail.com
Mon Oct 10 18:23:35 EDT 2011


On Thu, Sep 1, 2011 at 13:30, Peter Otten <__peter__ at web.de> wrote:
> Dotan Cohen wrote:
>
>> In the terrific Anki [1] application I am trying to remove trailing
>> whitespace from form fields. This is my regex:
>> [\n+\s+]$
>
> My attempt:
>
>>>> sub = re.compile(r"\s*?(\n|$)").sub
>>>> sub("<EOL>", "alpha   \nbeta   \r\n\ngamma\n")
> 'alpha<EOL>beta<EOL><EOL>gamma<EOL>'
>>>> sub("<EOL>", "alpha   \nbeta   \r\n\ngamma")
> 'alpha<EOL>beta<EOL><EOL>gamma<EOL>'
>>>> sub("<EOL>", "alpha   \nbeta   \r\n\ngamma\t")
> 'alpha<EOL>beta<EOL><EOL>gamma<EOL>'
>

Hi Peter, sorry for the _late_ reply.

It turns out that Anki stores newlines internally as <br>, since its
display model is based on HTML. Thanks, though!


-- 
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com



More information about the Python-list mailing list