Gnaneshwar Babu
Gnaneshwar Babu

Reputation: 111

Ignore special characters from a list excluding the mentioned characters

I had been using regex to ignore special characters from my list. But now I want to ignore special character excluding a few special characters mentioned by the user.

The code that I am currently using to remove special character is :

final_list=[re.sub('[^a-zA-Z0-9]+', '', _)for _ in a]

This works fine when I want to remove all the special characters in my list.

Input:

["on@3", "two#", "thre%e"]

output:

['on3', 'two', 'three']

But what my expectation is if I give ignore special characters except $#%

Input:

["on@3", "two#", "thre%e"]

output:

['on3', 'two#', 'thre%e']

This is my expected output

$#% is just for example. The user can mention any special character and I need the code to not remove the special character mentioned by the user but remove all other special characters.

Upvotes: 1

Views: 1211

Answers (2)

Hugo Delahaye
Hugo Delahaye

Reputation: 141

Just add the list of characters to the list.

import re

a = ["on@3", "two$", "thre%e"]

final_list = [re.sub('[^a-zA-Z0-9\$#%]+', '', _) for _ in a]

print final_list

outputs

['on3', 'two$', 'thre%e']

$ has a meaning in regular expressions so you need to escape it with a \

If you want to take user input, just use

import re

a = ["on@3", "two$", "thre%e"]

except_special_chars = input('Exceptions:')

final_list = [re.sub('[^a-zA-Z0-9'+str(except_special_chars)+']+', '', _) for _ in a]

print final_list

then the user input the special characters between quotes ' and with an escaping \ if necessary.

Upvotes: 0

JPG
JPG

Reputation: 88689

Add those charecters to the regex as

[re.sub('[^a-zA-Z0-9$#%]+', '', _)for _ in a]
                    ^^^

as @DYZ mentioned, you could also use '[^\w$#%]+' regex

[re.sub('[^\w$#%]+', '', _)for _ in a]

UPDATE-1

import re
a = ["on@3", "two#", "thre%e"]
special_char_to_be_removed = "%" # here you can change the values
regex = '[^\w{your_regex}]+'.format(your_regex=special_char_to_be_removed)
[re.sub(regex, '', _)for _ in a]

Upvotes: 2

Related Questions