I'd like to share some more insights and my reasoning when I searched for a workaround.
Main workaround idea is using two consecutive backreferences in the replacement.
I tried all backreference syntax described at Replacement Strings Reference: Matched Text and Backreferences. It appeared that none of \g<1>
, \g{1}
, ${1}
, $<1>
, $+{1}
, etc. work. However, there are some other backreferences, like $'
(inserts the portion of the string that follows the matched substring) or $`
(inserts the portion of the string that precedes the matched substring). However, these two backreferences do not work in VS Code file search and replace feature, they do not insert any text when used in the replacement pattern.
So, we may use $`
or $'
as empty placeholders in the replacement pattern.
Find What: fix(.*?)123
Replace With:
Or, as in my preliminary test, already provided in Mark's answer, a "technical" capturing group matching an empty string, ()
, can be introduced into the pattern so that a backreference to that group can be used as a "guard" before the subsequent "meaningful" backreference:
Find What: fixed()(.*)123
(see ()
in the pattern that can be referred to using $1
)
Replace With: fixed$1$2234
Here, $1
is a "guard" placeholder allowing correct parsing of $2
backreference.
Side note about named capturing groups
Named capturing groups are supported, but you should use .NET/PCRE/Java named capturing group syntax, (?<name>...)
. Unfortunately, the none of the known named backreferences work replacement pattern. I tried $+{name}
Boost/Perl syntax, $<name>
, ${name}
, none work.
Conclusion
So, there are several issues here that need to be addressed:
- We need an unambiguous numbered backerence syntax (
\g<1>
, ${1}
, or $<1>
)
- We need to make sure
$'
or $`
work as expected or are parsed as literal text (same as $_
(used to include the entire input string in the replacement string) or $+
(used to insert the text matched by the highest-numbered capturing group that actually participated in the match) backreferences that are not recognized by Visual Studio Code file search and replace feature), current behavior when they do not insert any text is rather undefined
- We need to introduce named backreference syntax (like
\g<name>
or ${name}
).