Reputation: 1631
I am trying to filter out inconsistent characters from a string.
Currently I have this..in python
name = re.sub('([^a-zA-Z0-9 -\'!$&])',' ', name)
But I am trying to include characters such as '()' brackets '/' backslash and forward slash. Somehow it does not work. Could anyone take a look please..
Upvotes: 0
Views: 69
Reputation: 350
A non-regex solution
accepted = '''!$*()\/.,>-_=+<:;'"?|'''
allowed = string.digits + string.letters + accepted
filter(allowed.__contains__, name)
This will filter the string name for non-alphanumeric characters and negate to filter the characters listed in accepted.
Upvotes: 1
Reputation: 1631
name = re.sub('([^a-zA-Z0-9\[\]\(,\)\+\/ \\-\'!$&])',' ', name)
Upvotes: 0