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
Please tick this box to confirm you have reviewed the above.
What version of ripgrep are you using?
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 stringsfoo
andbar
What is the actual behavior?
Lines that contain
foo
get printed asfoo
, as expected Lines that containbar
get printed as empty, which is rather surprising since neither of the patterns allow empty stringIf I switch the replacement to
-r '$2'
, I see:Lines that contain
foo
get printed as empty Lines that containbar
get printed asbar
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 asfoo
Lines that containbar
get printed asbar
As a workaround, I can achieve this behavior right now using
-r '$1$2'
which works but is rather unintuitiveMy 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