Regex to match all trailing whitespace _and_ newlines.

Discussion in 'Python' started by Dotan Cohen, Sep 1, 2011.

  1. Dotan Cohen

    Dotan Cohen Guest

    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
    Dotan Cohen, Sep 1, 2011
    #1
    1. Advertising

  2. Dotan Cohen

    Peter Otten Guest

    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>'
    Peter Otten, Sep 1, 2011
    #2
    1. Advertising

  3. Dotan Cohen

    Dotan Cohen Guest

    On Thu, Sep 1, 2011 at 13:30, Peter Otten <> 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
    Dotan Cohen, Oct 10, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Marts
    Replies:
    3
    Views:
    401
    Brad Marts
    Nov 17, 2003
  2. Russell Warren

    logging module and trailing newlines

    Russell Warren, Oct 2, 2007, in forum: Python
    Replies:
    2
    Views:
    266
    Russell Warren
    Oct 3, 2007
  3. John

    Removing trailing newlines -

    John, Apr 23, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    161
    J. Gleixner
    Apr 24, 2008
  4. Replies:
    6
    Views:
    130
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
  5. jwcarlton
    Replies:
    1
    Views:
    459
    Martin Honnen
    Feb 5, 2011
Loading...

Share This Page