-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong line numbers reported when using multiline search #2420
Comments
Potentially, this is of course could be me failing to understand the regular expression I am trying to use. My expectation is that |
It looks like your expected and actual output are the same? Or am I missing something? The problem here is definitely because of the replacement. It's not obvious to me how line numbers should actually be counted/displayed. In this case, there are two matches in your haystack. One contained to line 2 and one that is spread across lines 8 through 11. You've also told ripgrep, "replace the entire span of the match with
The really crucial bit of information here is what you expect to happen, but it looks like that was maybe you have a mistake there? |
That seems to be exactly what is happening here? |
@BurntSushi I've just updated the original post with correct expectation, not sure why I originally did it wrong 😞 I get this output:
But expect this output:
It seems I am failing to wrap my head around either there's an issue with my regexp, or why how line numbers are calculated in case of multiline match 🤔 From what you're saying, it sounds like the reported line numbers correspond to the whole matched string, e.g. starting with Is there a way the regexp could be re-written to match the conditions, e.g. to get a line location of string B, given string B is between strings A and C, even if all 3 are on different lines? |
There's really no way for ripgrep to know what you intend the line number to be. ripgrep just reports line numbers based on the match. Consider how your desire would scale to multiple capture groups, or even if your replacement was just a literal string and not a capture group at all. There's no clear and obvious rule that I can see that gets you what you want. The status quo might not always line up with your intuition, but it's a consistent rule: regardless of whether multiline is used or not, ripgrep replaces the entire match with the replacement given, and the line number corresponds to the line number of where the match started. (At least to me, that also matches my own intuition, because the replacement is actually deleting lines here.)
It's hard for me to parse this request as-is, but based on the context, I don't think so. There really is a fundamental issue here where you're replacing the entire contents of a match that spans several lines with a a single string that spans only one line. The line numbers have to be reconciled in some way. |
@BurntSushi fair enough, thank you! 👍 I think I understand the issue here, particularly, my wrong expectations towards what should be the line number. I'll have to think of a way to re-write my regex a bit more, or completely change the approach here. Closing the issue since obviously this doesn't look like a bug in ripgrep. |
Aye. It's worth pointing out here that grep tools, and in particular ripgrep's What I'm trying to say here is that if a simple one-liner with ripgrep doesn't get you what you want, then a simple 10-liner in Python (or whatever language) might be the way to go. :-) |
What version of ripgrep are you using?
How did you install ripgrep?
Via homebrew, e.g.:
What operating system are you using ripgrep on?
macOS 12.5.1 (21G83)
Describe your bug.
When using a multiline match, reported line numbers appear to not be correct.
What are the steps to reproduce the behavior?
Attempt to find out the line numbers of a
myapp
string, that is known to occur betweenapp_env
and:billing
strings.Using the following text:
What is the actual behavior?
The following output is observed:
What is the expected behavior?
The following output is expected:
The text was updated successfully, but these errors were encountered: