问题
I have a need to replace this:
fixed variable 123
with this:
fixed variable 234
In VSCode this matches fine:
fixed(.*)123
I can't find any way to make it put the capture in the output if a number follows:
fixed$1234
fixed${1}234
But the find replace window just looks like this:
I read that VSCode uses rust flavoured rexes.. Here indicates ${1}234
should work, but VSCode just puts it in the output..
Tried named capture in a style according to here
fixed(?P<n>.*)123 //"invalid regular expression" error
VSCode doesn't seem to understand ${1}
:
ps; I appreciate I could hack it in the contrived example with
FIND: fixed (.*) 123
REPL: fixed $1 234
And this does work in vscode:
but not all my data consistently has the same character before the number
回答1:
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:
fix$'$1234
fix$`$1234
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}
).
回答2:
After a lot of investigation by myself and @Wiktor we discovered a workaround for this apparent bug in vscode's search (aka find across files) and replace functionality in the specific case where the replace would have a single capture group followed by digits, like
$1234
where the intent is to replace with capture group 1 $1
followed by 234
or any digits. But $1234
is the actual undesired replaced output.
[This works fine in the find/replace widget for the current file but not in the find/search across files.]
There are (at least) two workarounds. Using two consecutive groups, like $1$2234
works properly as does $1$`234 (or precede with the $backtick).
So you could create a sham capture group as in (.*?)()(\d{3})
where capture group 2 has nothing in it just to get 2 consecutive capture groups in the replace or
use your intial search regex (.*?)(\d{3})
and then use $` just before or after your "real" capture group $1.
OP has filed an issue https://github.com/microsoft/vscode/issues/102221
Oddly, I just discovered that replacing with a single digit like $11
works fine but as soon as you add two or more it fails, so $112
fails.
来源:https://stackoverflow.com/questions/62850149/vscode-regex-find-replace-in-files-cant-get-a-numbered-capturing-group-followe