How to rermove non-alphanumeric characters at the beginning or end of a string

后端 未结 5 1396
故里飘歌
故里飘歌 2020-12-07 03:27

I have a list with elements that have unnecessary (non-alphanumeric) characters at the beginning or end of each string.

Ex.

\'cats--\'
5条回答
  •  悲哀的现实
    2020-12-07 03:39

    By using strip you have to know the substring to be stripped.

    >>> 'cats--'.strip('-')
    'cats'
    

    You could use re to get rid of the non-alphanumeric characters but you would shoot with a cannon on a mouse IMO. With str.isalpha() you can test any strings to contain alphabetic characters, so you only need to keep those:

    >>> ''.join(char for char in '#!cats-%' if char.isalpha())
    'cats'
    >>> thelist = ['cats5--', '#!cats-%', '--the#!cats-%', '--5cats-%', '--5!cats-%']
    >>> [''.join(c for c in e if c.isalpha()) for e in thelist]
    ['cats', 'cats', 'thecats', 'cats', 'cats']
    

    You want to get rid of non-alphanumeric so we can make this better:

    >>> [''.join(c for c in e if c.isalnum()) for e in thelist]
    ['cats5', 'cats', 'thecats', '5cats', '5cats']
    

    This one is exactly the same result you would get with re (as of Christian's answer):

    >>> import re
    >>> [re.sub("[^\\w]", "", e) for e in thelist]
    ['cats5', 'cats', 'thecats', '5cats', '5cats']
    

    However, If you want to strip non-alphanumeric characters from the end of the strings only you should use another pattern like this one (check re Documentation):

    >>> [''.join(re.search('^\W*(.+)(?!\W*$)(.)', e).groups()) for e in thelist]
    ['cats5', 'cats', 'the#!cats', '5cats', '5!cats']
    

提交回复
热议问题