The [^a-zA-Z0-9:):D)]
pattern is erronrous since it is a character class meant to match sequences of chars. You need to add an alternative to this regex that will match char sequences.
To remove any punctuation other than a certain list of smileys you may use
re.sub(r"(:-?[()D])|[^A-Za-z0-9\s]", r"\1" , s)
Or, in Python 3.4 and older, due to the re.sub
bug:
re.sub(r"(:-?[()D])|[^A-Za-z0-9,\s]", lambda x: x.group(1) if x.group(1) else "", s)
If you really need to avoid removing commas, add ,
into the negated character class:
re.sub(r"(:-?[()D])|[^A-Za-z0-9,\s]", r"\1" , s)
^
See the regex demo.
Details
(:-?[()D])
- matches and captures into Group 1 a :
, then an optional -
, and then a single char from the character class: (
, )
or D
(this captures the smileys like :-)
, :-(
, :)
, :(
, :-D
, :D
)
[^A-Za-z0-9,\s]
- matches any char but an ASCII letter, digit, comma and whitespace. To make it fully Unicode aware, replace with (?:[^\w\s,]|_)
.
See the Python 3.5+ demo:
import re
s = "Hi, this is good :)#"
print( re.sub(r"(:-?[()D])|[^A-Za-z0-9,\s]", r"\1" , s) )
# => Hi, this is good :)
See this Python 3.4- demo:
import re
s = "Hi, this is good :)#"
print( re.sub(r"(:-?[()D])|[^A-Za-z0-9,\s]", lambda x: x.group(1) if x.group(1) else "", s) )
# => Hi, this is good :)