ggreer / the_silver_searcher

A code-searching tool similar to ack, but faster.
http://geoff.greer.fm/ag/
Apache License 2.0
26.09k stars 1.42k forks source link

Wrong previous line match with patterns like '^[^=]+test' #713

Open skrattaren opened 9 years ago

skrattaren commented 9 years ago

Let's take the file

x = t.test()
#no
t.test()

nothing here
t.test()

---
EOF here, last line

I want to find all the invocations of method test() where result isn't assigned. So I use the following regexp:

 % ag '^[^=]+test'
test.file
2:#no
3:t.test()
4:
5:
6:nothing here
7:t.test()

Why do all those lines match? Both ack and egrep produce correct result:

 % egrep '^[^=]+test' test
t.test()
t.test()

Mac OS X Mavericks:

 % ag --version
ag version 0.30.0

Features:
  +jit +lzma +zlib
epmatsw commented 9 years ago

This is yet another side effect of ag not reading line-by-line. Because it looks at the entire text as a single buffer, it's seeing \n\n\nnothing here\nt.test(), which matches your regex. I don't really think there's a good way to work around that, but someone else may know more.

decaff commented 9 years ago

This issue pops up periodically. Here's a solution:

$ ag '^[^=\n]+test' test.file
3:t.test()
6:t.test()
$

Here's a proposed man page change that documents same.

skrattaren commented 9 years ago

Thanks, @decaff (esp. for the workaround) and @epmatsw, hadn't realized this is an issue with multiline buffer, otherwise I've found earlier issues Should I close this issue as duplicate of #459, then?