BurntSushi / ripgrep

ripgrep recursively searches directories for a regex pattern while respecting your gitignore
The Unlicense
48.77k stars 2.01k forks source link

surprising behavior of replacement using number capture groups when combined with multiple search patterns #2935

Open hugues-aff opened 2 hours ago

hugues-aff commented 2 hours ago

Please tick this box to confirm you have reviewed the above.

What version of ripgrep are you using?

ripgrep 14.1.1 (rev 4649aa9700)

features:+pcre2
simd(compile):+NEON
simd(runtime):+NEON

How did you install ripgrep?

homebrew

What operating system are you using ripgrep on?

macOS 14.7

Describe your bug.

ripgrep behavior when combining multiple patterns and replacement strings using numbered capture groups is very surprising.

What are the steps to reproduce the behavior?

Run rg -e '(foo)' -e '(bar)' -r '$1' -o in a directory with files containing strings foo and bar

What is the actual behavior?

Lines that contain foo get printed as foo, as expected Lines that contain bar get printed as empty, which is rather surprising since neither of the patterns allow empty string

If I switch the replacement to -r '$2', I see:

Lines that contain foo get printed as empty Lines that contain bar get printed as bar

What is the expected behavior?

I would expect the replacement index to reference whichever pattern actually matched, resulting in:

Lines that contain foo get printed as foo Lines that contain bar get printed as bar

As a workaround, I can achieve this behavior right now using -r '$1$2' which works but is rather unintuitive

My preferred solution would be to allow a 1:1 mapping for -r to -e, but failing that, it seems like the indexing into capture groups when given multiple patterns should either be changed to be more intuitive, or at least documented if we're worried about breaking backwards compatibility

BurntSushi commented 2 hours ago

Please provide an MRE.