Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regex to match all trailing whitespace _and_ newlines.

Reply
Thread Tools

Regex to match all trailing whitespace _and_ newlines.

 
 
Dotan Cohen
Guest
Posts: n/a
 
      09-01-2011
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-01-2011
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>'

 
Reply With Quote
 
 
 
 
Dotan Cohen
Guest
Posts: n/a
 
      10-10-2011
On Thu, Sep 1, 2011 at 13:30, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex question for String.replace; match whitespace OR nothing jwcarlton Javascript 1 02-05-2011 09:39 AM
Strip is not stripping trailing whitespace Taylor Strait Ruby 13 12-28-2006 05:04 PM
QUERY: strip trailing whitespace ... woes :S KevinGPO C++ 1 02-20-2006 01:48 PM
remove trailing whitespace from string Donald Canton C++ 5 02-09-2004 04:39 PM
Reading words with trailing whitespace Brad Marts C++ 3 11-17-2003 11:31 PM



Advertisments