I'm trying to use a regex to clean some data before I insert the items into the database. I haven't been able to solve the issue of removing trailing special characters at the end of my strings.
How do I write this regex to only remove trailing special characters?
import re strings = ['string01_','str_ing02_^','string03_@_', 'string04_1', 'string05_a_'] for item in strings: clean_this = (re.sub(r'([_+!@#$?^])', '', item)) print (clean_this) outputs this: string01 # correct string02 # incorrect because it remove _ in the string string03 # correct string041 # incorrect because it remove _ in the string string05a # incorrect because it remove _ in the string and not just the trailing _
[_+!@#$?^]+$