Ack with regex - Confusion with simple queries

Amelio Vazquez-Reina picture Amelio Vazquez-Reina · Jul 18, 2011 · Viewed 12.1k times · Source

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.

Answer

Konrad Rudolph picture Konrad Rudolph · Jul 18, 2011

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”.