Ack with regex - Confusion with simple queries
Asked Answered
Y

1

16

I am trying to use ack-is-better-than-grep (ack) with regular expressions to find lines and snippets in my code repository. My understanding is that ack uses Perl-derivative regular expressions, is that correct?

However, I am not sure how would these queries work:

ack 'foo'
ack '.*(foo)+.*'

ack '.*foo'
ack 'foo.*'

Could they give different outputs? If so, why?

EDIT: In my tests they output different results (the first one outputs more matches than the others, for example). They also highlight different parts of the same lines.

EDIT 2: The difference in the output apparently is related to the highlighting (the coloring of the output). I have noticed that if I run ack with --nocolor the output of the commands above are the same. Apparently running ack with the default coloring makes part of the output invisible in my machine/config. I am running it on a GNOME terminal from bash in Ubuntu 11.04.

Yoghurt answered 18/7, 2011 at 19:9 Comment(3)
Do these queries give different outputs? If so, can you please give an example? Otherwise, what is your reason to suspect that they do?Obscuration
What do you mean by the first one outputs more matches? Which lines are matched by 1 but not matched by the others?Willamina
Thanks @Konrad and @inTide, I have updated the question to address your comments.Yoghurt
O
18

My understanding is that ack uses Perl-derivative regular expressions, is that correct?

Yes. In fact, ack is written in Perl and just forwards the expressions to the internal engine.

Could they give different outputs? If so, why?

The expressions should find the same results. However, the output might still differ since ack allows syntax highlighting in its output (by default; unless --nocolor is specified) – it uses ANSI escape codes to colour hits in the output and obviously 'foo' will highlight up something different than '.*foo' (and the other queries).

Namely, in the following input:

This is a foo test.

a query for 'foo' will highlight just that: “foo”, while '.*foo' will highlight “This is a foo”.

Obscuration answered 18/7, 2011 at 19:20 Comment(1)
Thanks @Konrad, that explains the differences in highlighting. I do however get different outputs for them, BUT when I run them with the --nocolor option they do output the same result. I think the problem is that there is a problem with the ANSI escape codes in some of the outputs that makes the matchings invisible (there is black space in the output for some of them)Yoghurt

© 2022 - 2024 — McMap. All rights reserved.