beyondgrep / ack2

**ack 2 is no longer being maintained. ack 3 is the latest version.**
https://github.com/beyondgrep/ack3/
Other
1.48k stars 138 forks source link

Lookbehind sometimes causing wrong part of line to be highlighted #374

Closed elias6 closed 7 years ago

elias6 commented 11 years ago

I am running ack 2.08 on OS X 10.8.4. If I run echo 'xy' | ack '(?<=x)y' from the terminal, it prints xy with the y highlighted, like I would expect. But if I run echo 'xy' | ack '(?<=(x))y', then it prints xy with the x highlighted. Is this intended behavior or a bug? I would think the y is the actual match and the x is not part of it. The grouping should have nothing to do with it.

packy commented 11 years ago

Though, if I run echo 'xy' | ack '(?<=(?:x))y', then it prints xy with the y highlighted. The regular expression capturing x must confuse ack 2.08 somehow (I'm on OS X 10.6.8, but I was able to reproduce the problem, so I don't think OS version is part of the problem).

Just for giggles, I tried it with a copy of ack 1.94 I had lying around, and echo 'xy' | ack '(?<=x)y', echo 'xy' | ack '(?<=(x))y' and echo 'xy' | ack '(?<=(?:x))y' all printed xy with the y highlighted.

hoelzro commented 11 years ago

@mikez302 Thanks for the bug report! I'll take a more in-depth look later today.

n1vux commented 7 years ago

Yes, Andy has said elsewhere that he considers highlighting $1 from capturing parens instead of always highlighting $& to be a known bug in ack2, which will be fixed in ack3 .

This also means ack2 'x(y)' will highlight the same as ack2 '(?<=x)y' ( but it won't in ack3 ).

The workaround for now is as @packy notes to use non-capturing parens (?:...) if you need parens but don't want to focus the highlight there.

petdance commented 7 years ago

This is fixed in ack 3. I'm leaving the feature in ack 2 as-is.