ack regex:在同一行中按顺序匹配两个单词 [英] ack regex: Matching two words in order in the same line

查看:100
本文介绍了ack regex:在同一行中按顺序匹配两个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件中依次包含两个单词,分别为 word_1 word_2 的行,例如在下面的 A行中,但与在 B行 C行

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

我尝试过

$ack '*word_1*word_2'
$ack '(word_1)+*(word_2)+'

以及与 ^ 附加在正则表达式的开头(试图遵循Perl正则表达式的语法)。

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.

我在做什么错了?

谢谢!

推荐答案

您要查找 word_1 ,后跟任意次数,任意次数,后跟 word_2 。应该是

You want to find word_1, followed by anything, any number of times, followed by word_2. That should be

word_1.*word_2

您似乎正在使用 * ,因为它经常在命令行搜索中使用,但在正则表达式中它是前一个字符的量词,表示至少匹配0次。例如,正则表达式 a * 将匹配0个或多个 a s,而正则表达式 a + 至少匹配一个 a

You 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 regex a* would match 0 or more as, whereas the regex a+ would match at least one a.

正则表达式元字符的意思是匹配任何内容是,所以。* 的意思是匹配任何次数,任何次数。请参阅 perlrequick ,以简要介绍该主题。

The regex metacharacter meaning "match anything" is ., so .* means "match anything, any number of times. See perlrequick for a brief introduction on the topic.

这篇关于ack regex:在同一行中按顺序匹配两个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆