regexregex-lookaroundsag

how to match all files containing word1 AND word2 across different lines with ag or rg (PCRE/Rust regex)


I've long list of generated reports which I want to filter. The report is something like this:

Report Name
Report Date
Blah blah blah
Blah: WORD1
Blah blah
blah blah: WORD2
blah blah

I'm trying to use ag (PCRE regex) or rg (rust regex) and find all files which contains WORD1 AND WORD2 in different places of the file (contains new line).

I've already search SX and found these which didn't work:

> ag (?=.*WORD1)(?=.*WORD2)

> ag (?=.*WORD1)((.|\n)*)(?=.*WORD2)

UPDATE

As @WiktorStribiżew pointed out, the ag uses PCRE. Sorry for the mistake.

my expected output is:

blah blah: WORD2

or just the list of matched files.


p.s. currently I've managed to using this:

> ag "WORD2" $(ag -l "WORD1")

Solution

  • You may use a PCRE pattern with ag:

    (?s)^(?=.*WORD1)(?=.*WORD2).*\n\K(?-s).*WORD2
    

    See the regex demo.

    Details: