И повставляйте символы в ILLEGAL_CHARS
import re test_str = 'so|m}e:te^xt\\31\K`-34//>' ILLEGAL_CHARS = "[<>`{}^&:/\\|?*\"]|[\0-\31]" def __removeIllegalChars(name): return re.sub(ILLEGAL_CHARS, "", name) print(__removeIllegalChars(test_str)) sometext\31\K-34 Process finished with exit code 0