Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 1.51 KB

negative-look-ahead-search-with-ripgrep.md

File metadata and controls

40 lines (31 loc) · 1.51 KB

Negative Look-Ahead Search With ripgrep

I have a huge monorepo with a bunch of instances of the NEXT_PUBLIC_SANITY_DATASET env var that need to be updated to NEXT_PUBLIC_SANITY_DATASET_ID (notice the _ID appended to the end). If I do a basic search for NEXT_PUBLIC_SANITY_DATASET, I get a huge list of results cluttered with all the instances where this env var has already been updated.

To get a list of only the places where the old env var, and not the new env var, appear, I need to use a regex feature called a negative look-ahead. That looks something like PATTERN(?!NLA) where we want to match on PATTERN, but not if it is immediately followed by NLA.

Let's try that with ripgrep:

$ rg 'NEXT_PUBLIC_SANITY_DATASET(?!_ID)' --hidden --glob '!node_modules/**' --glob '!.git/**'
regex parse error:
    NEXT_PUBLIC_SANITY_DATASET(?!_ID)
                              ^^^
error: look-around, including look-ahead and look-behind, is not supported

Consider enabling PCRE2 with the --pcre2 flag, which can handle backreferences
and look-around.

It doesn't work as is, but the error message helpfully tells me that I need to include the --pcre2 flag for look-ahead to work.

Let's try that again:

❯ rg --pcre2 'NEXT_PUBLIC_SANITY_DATASET(?!_ID)' --hidden --glob '!node_modules/**' --glob '!.git/**'
apps/testing-javascript/.env.development
37:NEXT_PUBLIC_SANITY_DATASET=production
...

That works!