0
我有一个Python字符串,它看起来像这样:如何删除特殊字符在python
str='(/|\\)cmd\.exe$'
现在我想删除特殊字符及以下为得到的字符串,在:
new_str=replace_func(str)
print new_str
cmd.exe
有人可以帮助我,如何编写这个replace_func函数。 在此先感谢!
我有一个Python字符串,它看起来像这样:如何删除特殊字符在python
str='(/|\\)cmd\.exe$'
现在我想删除特殊字符及以下为得到的字符串,在:
new_str=replace_func(str)
print new_str
cmd.exe
有人可以帮助我,如何编写这个replace_func函数。 在此先感谢!
试试这个:
string = "(/|\\)cmd\.exe$"
chars_to_remove = ("(|\\/$")
for char in chars_to_remove:
string=string.replace(char, "")
print string
使用正则表达式,你可以删除特殊字符
这里是一个正则表达式匹配的字符串是不是一个字母或数字:
[^A-Za-z0-9]+
例如:
import re
str='(/|\\)cmd\.exe$'
re.sub('[^A-Za-z0-9.]+', '',str)
如果您可以指定这些特殊字符的含义,将会有所帮助。我猜想str是一个正则表达式模式,在这种情况下,您可以使用它的匹配组来获取匹配的字符串。 – 2012-07-23 08:59:32