I try to do a substitution from a vim script and to operate over a captured group like so:
let string = "{b1} {b2} ({b3})"
echo substitute(string, "{\([^}]*\)}", "a", "g")
It doesn't match anything and the result doesn't change.
If I remove the \( \)
:
echo substitute(string, "{[^}]*}", "a", "g")
Then the whole {b1}
is replaced with a
, when I only want to replace the content of it: {a}
.
I have read that the pattern in the substitute command always work in magic
mode. And that in the magic mode, the capture group is: \( \)
.
Do you know the trick to make this work?
Edit:
Thanks to Christian Brabandt I was able to make it work (see his answer below). I had to change the \( \)
to \zs \ze
also.