python - How can I remove all non-alphanumeric characters from a string, except for '#', with regex? -


i have line address = re.sub('[^a-za-z0-9]+', ' ', address).lstrip() remove special characters string address. how can modify line keep #?

in order avoid removing hash symbol, need add negated character class:

r'[^a-za-z0-9#]+'              ^ 

see regex demo


Comments