I am not able to replace the string “(-)” using re.sub in Python.
>>> instr = 'Hello, this is my instring'
>>> re.sub('my', 'your', instr)
'Hello, this is your instring'
>>> instr = 'Hello, this is my (-) instring'
>>> re.sub('my (-)', 'your', instr)
'Hello, this is my (-) instring'
Can somebody please give me a hint what I am doing wrong.
Thank you!
You have to escape the parenthesis, which are normally used for matching groups. Also, add an
rin front of the string to keep it raw (because of backslashes).Or don’t use regexp at all (if your substitution is that simple) and you don’t have to care about many issues: