Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何删除python中的特殊字符_Python 2.7 - Fatal编程技术网

Python 2.7 如何删除python中的特殊字符

Python 2.7 如何删除python中的特殊字符,python-2.7,Python 2.7,我有一个python字符串,如下所示: str='(/|\\)cmd\.exe$' 现在,我想删除特殊字符并获取以下中的字符串: new_str=replace_func(str) print new_str cmd.exe 有人能帮我写这个替换函数吗。 提前谢谢 试试这个: string = "(/|\\)cmd\.exe$" chars_to_remove = ("(|\\/$") for char in chars_to_remove: string=string.repla

我有一个python字符串,如下所示:

str='(/|\\)cmd\.exe$'
现在,我想删除特殊字符并获取以下中的字符串:

new_str=replace_func(str)
print new_str
cmd.exe
有人能帮我写这个替换函数吗。 提前谢谢

试试这个:

string = "(/|\\)cmd\.exe$"

chars_to_remove = ("(|\\/$")
for char in chars_to_remove:
    string=string.replace(char, "")

print string

使用正则表达式可以删除特殊字符

下面是一个正则表达式,用于匹配非字母或数字的字符串:

[^A-Za-z0-9]+
例:


如果您能详细说明这些特殊字符的确切含义,这将非常有用。我猜str是一个regex模式,在这种情况下,您可以使用它的匹配组来获取它匹配的字符串。
import re
str='(/|\\)cmd\.exe$'
re.sub('[^A-Za-z0-9.]+', '',str)