ack regex:按顺序匹配同一行中的两个单词
我想在文件中查找按顺序包含两个单词 word_1
和 word_2
的行,例如下面的 Line A
中,但不是这样在 Line B
或 Line C
中:
Line A: ... word_1 .... word_2 ....
Line B: ... word_1 ....
Line C: ... word_2 ....
我尝试
$ack '*word_1*word_2'
$ack '(word_1)+*(word_2)+'
过在正则表达式的开头附加相同的命令 ^
(试图遵循Perl 正则表达式语法)。
这些命令都没有返回我感兴趣的文件或行。
我做错了什么?
谢谢!
I would like to find lines in files that include two words, word_1
and word_2
in order, such as in Line A
below, but not as in Line B
or Line C
:
Line A: ... word_1 .... word_2 ....
Line B: ... word_1 ....
Line C: ... word_2 ....
I have tried
$ack '*word_1*word_2'
$ack '(word_1)+*(word_2)+'
and the same commands with ^
appended at the beginning of the regex (in an attempt to follow the Perl regex syntax).
None of these commands return the files or the lines I am interested in.
What am I doing wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要查找
word_1
,后跟任意次数的任何内容,最后是word_2
。那应该是你似乎正在使用
*
因为它经常在命令行搜索中使用,但在正则表达式中它是前面字符的量词,意味着匹配它至少 0 次。例如,正则表达式a*
将匹配 0 个或多个a
,而正则表达式a+
将匹配至少一个a< /代码>。
表示“匹配任何内容”的正则表达式元字符是
.
,因此.*
表示“匹配任何内容,任意次数。请参阅 perlrequick 有关该主题的简要介绍。You want to find
word_1
, followed by anything, any number of times, followed byword_2
. That should beYou seem to be using
*
as it is often used in command line searches, but in regexes is it a quantifier for the preceding character, meaning match it at least 0 times. For example, the regexa*
would match 0 or morea
s, whereas the regexa+
would match at least onea
.The regex metacharacter meaning "match anything" is
.
, so.*
means "match anything, any number of times. See perlrequick for a brief introduction on the topic.