I want to search for a substring in a string and replace with another pattern.
The search string is in between curly braces.
Example string
line = $lang['MY_KEY'] = '{search-string} wird in Analyse berücksichtigt';
I tried this code:
re.sub('([a-zA-Z0-9\[\]\$\' ].*{).*(}[a-zA-Z\.,; ].*)','\1replace-string\2',line)
And also:
re.sub('(.*{).*(}.*)', '\1replace-string\2', line)
I am getting some junk characters along with the replace-string in the output.
EDIT
I have one more question.
There are multiple search strings in curly braces. I want to replace each substring with different patterns. How can I do it?
As you have backslashes prefix the patterns and replacements with an r :-
EDIT: replace every second occurrence using a replacement function
EDIT: Use a dictionary to store the replacements