Re: stripping a string

Discussion in 'Python' started by Jeff Epler, Sep 14, 2003.

  1. Jeff Epler

    Jeff Epler Guest

    If you want to remove all digits from the string, then use
    str.translate, not regular expressions:
    import string
    identity_transformation = string.maketrans('', '')

    def remove_digits(s):
    return s.translate(identity_transformation, string.digits)

    >>> s = 'ANL LN32'
    >>> remove_digits(s)

    'ANL LN'

    If you want to remove digits from the end of the string, then use
    str.strip(), not a regular expression:
    >>> s.rstrip(string.digits)

    'ANL LN'

    Jeff
    Jeff Epler, Sep 14, 2003
    #1
    1. Advertising

  2. Jeff Epler

    Tim Williams Guest

    Jeff Epler <> wrote in message news:<>...
    > If you want to remove all digits from the string, then use
    > str.translate, not regular expressions:
    > import string
    > identity_transformation = string.maketrans('', '')
    >
    > def remove_digits(s):
    > return s.translate(identity_transformation, string.digits)
    >
    > >>> s = 'ANL LN32'
    > >>> remove_digits(s)

    > 'ANL LN'
    >
    > If you want to remove digits from the end of the string, then use
    > str.strip(), not a regular expression:
    > >>> s.rstrip(string.digits)

    > 'ANL LN'
    >
    > Jeff


    I don't understand. What's wrong with

    >>> import re
    >>> s = 'ANL LN32'
    >>> s=re.sub('[0-9]', '', s)
    >>> print s

    ANL LN
    Tim Williams, Sep 15, 2003
    #2
    1. Advertising

  3. Jeff Epler

    Peter Otten Guest

    Tim Williams wrote:

    > Jeff Epler <> wrote in message
    > news:<>...
    >> If you want to remove all digits from the string, then use
    >> str.translate, not regular expressions:
    >> import string
    >> identity_transformation = string.maketrans('', '')
    >>
    >> def remove_digits(s):
    >> return s.translate(identity_transformation, string.digits)
    >>
    >> >>> s = 'ANL LN32'
    >> >>> remove_digits(s)

    >> 'ANL LN'
    >>
    >> If you want to remove digits from the end of the string, then use
    >> str.strip(), not a regular expression:
    >> >>> s.rstrip(string.digits)

    >> 'ANL LN'
    >>
    >> Jeff

    >
    > I don't understand. What's wrong with
    >
    >>>> import re
    >>>> s = 'ANL LN32'
    >>>> s=re.sub('[0-9]', '', s)
    >>>> print s

    > ANL LN


    Nothing is *wrong*, but

    >>> import re
    >>> s = 'AN21 LN32'
    >>> print re.sub('[0-9]', '', s)

    AN LN

    whereas

    >>> import string
    >>> print s.rstrip(string.digits)

    AN21 LN

    is *different*.

    translate() gives the same result as re.sub(), but should be much faster
    (haven't timed it though).

    Peter
    Peter Otten, Sep 15, 2003
    #3
  4. Peter Otten wrote:
    > Tim Williams wrote:
    > >
    > > I don't understand. What's wrong with
    > >
    > >>>> import re
    > >>>> s = 'ANL LN32'
    > >>>> s=re.sub('[0-9]', '', s)
    > >>>> print s

    > > ANL LN

    >
    > Nothing is *wrong*, but
    >
    > >>> import re
    > >>> s = 'AN21 LN32'


    [...]

    > >>> import string
    > >>> print s.rstrip(string.digits)

    > AN21 LN
    >
    > is *different*.


    In that case, you could do:

    >>> import re
    >>> s = 'AN21 LN32'
    >>> print re.sub('[0-9]*$', '', s)

    AN21 LN

    > translate() gives the same result as re.sub(), but should be much faster
    > (haven't timed it though).


    I can never remember off the top of my head how to use maketrans or whatever
    it's called so that I can use translate (it's not that it's hard, it's
    simply that I can never remember it without looking it up). Despite their
    flaws, I find it easier to remember how to write regular expressions.

    -Andrew.
    Andrew Bennetts, Sep 16, 2003
    #4
  5. Jeff Epler

    Tim Williams Guest

    Andrew Bennetts <> wrote in message news:<>...
    > Peter Otten wrote:
    > > Tim Williams wrote:
    > > >
    > > > I don't understand. What's wrong with
    > > >
    > > >>>> import re
    > > >>>> s = 'ANL LN32'
    > > >>>> s=re.sub('[0-9]', '', s)
    > > >>>> print s
    > > > ANL LN

    > >
    > > Nothing is *wrong*, but
    > >
    > > >>> import re
    > > >>> s = 'AN21 LN32'

    >
    > [...]
    >
    > > >>> import string
    > > >>> print s.rstrip(string.digits)

    > > AN21 LN
    > >
    > > is *different*.

    >
    > In that case, you could do:
    >
    > >>> import re
    > >>> s = 'AN21 LN32'
    > >>> print re.sub('[0-9]*$', '', s)

    > AN21 LN
    >
    > > translate() gives the same result as re.sub(), but should be much faster
    > > (haven't timed it though).

    >
    > I can never remember off the top of my head how to use maketrans or whatever
    > it's called so that I can use translate (it's not that it's hard, it's
    > simply that I can never remember it without looking it up). Despite their
    > flaws, I find it easier to remember how to write regular expressions.
    >
    > -Andrew.


    I see the difference. I thought the original poster wanted to strip
    out the digits everywhere, not just at the end, but in looking back at
    the other posts, I saw that that was another posting. The original
    post wasn't specific about stripping from the end or not. (Except for
    the example.)
    Tim Williams, Sep 16, 2003
    #5
    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. Leeds, Mark

    stripping a string

    Leeds, Mark, Sep 13, 2003, in forum: Python
    Replies:
    1
    Views:
    316
    Uwe Schmitt
    Sep 16, 2003
  2. joram gemma

    string.lstrip stripping too much?

    joram gemma, May 15, 2005, in forum: Python
    Replies:
    4
    Views:
    385
    M.E.Farmer
    May 16, 2005
  3. orangeDinosaur

    string stripping issues

    orangeDinosaur, Mar 2, 2006, in forum: Python
    Replies:
    7
    Views:
    352
    Larry Bates
    Mar 3, 2006
  4. Edward Elliott

    stripping unwanted chars from string

    Edward Elliott, May 4, 2006, in forum: Python
    Replies:
    7
    Views:
    406
    Alex Martelli
    May 4, 2006
  5. Raj
    Replies:
    7
    Views:
    917
    Joe Wright
    May 11, 2005
Loading...

Share This Page