开发者

Ack with regex - Confusion with simple queries

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 开发者_如何学Pythonnot 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜