print(re.sub('(\\-|\\]|\\^|\\$|\\*|\\.|\\\\)', lambda m: {'-': '\\-', ']':
    '\\]', '\\': '\\\\', '^': '\\^', '$': '\\$', '*': '\\*', '.': '\\.'}[m.
    group()], '^stack.*/overflo\\w$arr=1'))
How to escape special characters of a string with single backslashes