0

Given the following text:

defmodule MyModule do
  app_env(:plans, :myapp, [:billing, :plans],
    binding_order: [:config],
    required: true,
    type: :any
  )

  app_env(
    :plans_with_min_amount_of_integrations,
    :myapp,
    [:billing, :plans_with_min_amount_of_integrations],
    binding_order: [:config],
    required: true,
    type: :any
  )
end

I'm trying to match with the following condition in mind:

  • myapp string,
  • that is known to be located between app_env and :billing strings.

To do this, I'm running:

rg --replace '$1' --multiline --multiline-dotall "app_env.*?(myapp).*?billing" test.txt

I expect the following output

2:myapp
10:myapp

But for some reason, I'm getting the following output:

2:myapp
8:myapp

Why? How do I change the regexp to return the correct lines, while retaining the conditions?

Note, that this example is simplified, and is a part of a larger code search effort of precisely looking & replacing the code, so simply running rg myapp won't cut it in this case.

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99

1 Answers1

0

Author kindly responded to issue in ripgrep bug tracker about this: https://github.com/BurntSushi/ripgrep/issues/2420

It's not an issue with ripgrep, but rather my own faulty expectations based on misunderstanding how line numbers in conjunction with capture groups are calculated.

In a nutshell, a line number always corresponds to the first line of matched string, and not to the first capture group (which I wrongly assumed was the case).

Kudoes goes to @burntsushi5!

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99