忽略列表中不包括上述字符的特殊字符



我一直在使用正则表达式来忽略列表中的特殊字符。但是现在我想忽略特殊字符,不包括用户提到的一些特殊字符。

我目前用于删除特殊字符的代码是:

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

当我想删除列表中的所有特殊字符时,这工作正常。

输入:

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

输出:

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

但是我的期望是,如果我给忽略特殊字符,除了$#%

输入:

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

输出:

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

这是我的预期输出

$#%只是一个例子。用户可以提及任何特殊字符,我需要代码不删除用户提到的特殊字符,而是删除所有其他特殊字符。

将这些 charecter 添加到正则表达式中作为

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

正如@DYZ提到的,您也可以使用正则表达式'[^w$#%]+'

[re.sub('[^w$#%]+', '', _)for _ in a]
>更新-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]

只需将字符列表添加到列表中即可。

import re
a = ["on@3", "two$", "thre%e"]
final_list = [re.sub('[^a-zA-Z0-9$#%]+', '', _) for _ in a]
print final_list

输出

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

$在正则表达式中具有含义,因此您需要使用对其进行转义

如果您想接受用户输入,只需使用

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

然后,用户在引号之间输入特殊字符,'并在必要时使用转义

最新更新