Replace non alphanumeric characters except some exceptions python

后端 未结 3 1379
暖寄归人
暖寄归人 2021-02-14 12:02

In perl s/[^\\w:]//g would replace all non alphanumeric characters EXCEPT :

In python I\'m using re.sub(r\'\\W+\', \'\',mystring)

3条回答
  •  离开以前
    2021-02-14 12:39

    You can specify everything that you need not remove in the negated character clas.

    re.sub(r'[^\w'+removelist+']', '',mystring)
    

    Test

    >>> import re
    >>> removelist = "=."
    >>> mystring = "asdf1234=.!@#$"
    >>> re.sub(r'[^\w'+removelist+']', '',mystring)
    'asdf1234=.'
    

    Here the removelist variable is a string which contains the list of all characters you need to exclude from the removal.

    What does negated character class means

    When the ^ is moved into the character class it does not acts as an anchor where as it negates the character class.

    That is ^ in inside a character class say like [^abc] it negates the meaning of the character class.

    For example [abc] will match a b or c where as [^abc] will not match a b or c. Which can also be phrased as anything other than a b or c

提交回复
热议问题