Abul Hasnat
Abul Hasnat

Reputation: 1631

regex help in python with alphanumaric

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

Answers (2)

cinelli
cinelli

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

Abul Hasnat
Abul Hasnat

Reputation: 1631

name = re.sub('([^a-zA-Z0-9\[\]\(,\)\+\/ \\-\'!$&])',' ', name)

Upvotes: 0

Related Questions