使用正则表达式确认 - 简单查询的混乱
我正在尝试将 ack-is-better-than-grep (ack) 与正则表达式结合使用来查找代码存储库中的行和片段。我的理解是 ack 使用 Perl 派生正则表达式,这是正确的吗?
但是,我不确定这些查询将如何工作:
ack 'foo'
ack '.*(foo)+.*'
ack '.*foo'
ack 'foo.*'
它们能否给出不同的输出?如果是这样,为什么?
编辑:在我的测试中,它们输出不同的结果(例如,第一个输出比其他结果更多的匹配)。它们还突出显示同一行的不同部分。
编辑2:输出的差异显然与突出显示(输出的颜色)有关。我注意到,如果我使用 --nocolor
运行 ack
,上述命令的输出是相同的。显然,使用默认颜色运行 ack 会使部分输出在我的机器/配置中不可见。我在 Ubuntu 11.04 中的 bash 的 GNOME 终端上运行它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。事实上,ack 是用 Perl 编写的,只是将表达式转发到内部引擎。
这些表达式应该找到相同的结果。然而,输出可能仍然不同,因为
ack
允许在其输出中突出显示语法(默认情况下;除非指定--nocolor
)——它使用 ANSI 转义码对输出显然'foo'
将突出显示与'.*foo'
(以及其他查询)不同的内容。也就是说,在以下输入中:
对
'foo'
的查询将突出显示:“foo
”,而'.*foo'
将突出显示:突出显示“这是一个 foo
”。Yes. In fact,
ack
is written in Perl and just forwards the expressions to the internal engine.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:
a query for
'foo'
will highlight just that: “foo
”, while'.*foo'
will highlight “This is a foo
”.