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