Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12555 > unrolled thread

Regex to match all trailing whitespace _and_ newlines.

Started byDotan Cohen <dotancohen@gmail.com>
First post2011-09-01 12:42 +0300
Last post2011-10-11 00:23 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Regex to match all trailing whitespace _and_ newlines. Dotan Cohen <dotancohen@gmail.com> - 2011-09-01 12:42 +0300
    Re: Regex to match all trailing whitespace _and_ newlines. Peter Otten <__peter__@web.de> - 2011-09-01 12:30 +0200
      Re: Regex to match all trailing whitespace _and_ newlines. Dotan Cohen <dotancohen@gmail.com> - 2011-10-11 00:23 +0200

#12555 — Regex to match all trailing whitespace _and_ newlines.

FromDotan Cohen <dotancohen@gmail.com>
Date2011-09-01 12:42 +0300
SubjectRegex to match all trailing whitespace _and_ newlines.
Message-ID<mailman.653.1314870127.27778.python-list@python.org>
In the terrific Anki [1] application I am trying to remove trailing
whitespace from form fields. This is my regex:
[\n+\s+]$

Actually, even simplifying it to [\n] or [\r\n] is not matching any
newlines! What might be the cause of this? Note that I am not entering
the regex in Python code, I am entering it in a regex-supporting
Find/Replace dialogue in Anki. Anki is written in Python.

Thanks.

[1] ankisrs.net

-- 
Dotan Cohen

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

[toc] | [next] | [standalone]


#12556

FromPeter Otten <__peter__@web.de>
Date2011-09-01 12:30 +0200
Message-ID<j3nmqm$vp4$1@solani.org>
In reply to#12555
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>'

[toc] | [prev] | [next] | [standalone]


#14417

FromDotan Cohen <dotancohen@gmail.com>
Date2011-10-11 00:23 +0200
Message-ID<mailman.1871.1318285416.27778.python-list@python.org>
In reply to#12556
On Thu, Sep 1, 2011 at 13:30, Peter Otten <__peter__@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web