From c2e4a2962961b760f0fe47fa3228eafceff8e2b8 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 28 Apr 2023 09:04:29 -0500 Subject: [PATCH] Add Negative Look-Ahead Search With ripgrep as a Unix TIL --- README.md | 3 +- ...negative-look-ahead-search-with-ripgrep.md | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 unix/negative-look-ahead-search-with-ripgrep.md diff --git a/README.md b/README.md index 26aac02..6037ac8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1296 TILs and counting..._ +_1297 TILs and counting..._ --- @@ -1282,6 +1282,7 @@ _1296 TILs and counting..._ - [List The Available JDKs](unix/list-the-available-jdks.md) - [List The Stack Of Remembered Directories](unix/list-the-stack-of-remembered-directories.md) - [Map A Domain To localhost](unix/map-a-domain-to-localhost.md) +- [Negative Look-Ahead Search With ripgrep](unix/negative-look-ahead-search-with-ripgrep.md) - [Occupy A Local Port With Netcat](unix/occupy-a-local-port-with-netcat.md) - [Only Show The Matches](unix/only-show-the-matches.md) - [Open The Current Command In An Editor](unix/open-the-current-command-in-an-editor.md) diff --git a/unix/negative-look-ahead-search-with-ripgrep.md b/unix/negative-look-ahead-search-with-ripgrep.md new file mode 100644 index 0000000..2fc4f96 --- /dev/null +++ b/unix/negative-look-ahead-search-with-ripgrep.md @@ -0,0 +1,40 @@ +# 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](https://www.regular-expressions.info/lookaround.html). 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](https://github.com/BurntSushi/ripgrep): + +```bash +$ 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: + +```bash +❯ 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!