mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Negative Look-Ahead Search With ripgrep as a Unix TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
40
unix/negative-look-ahead-search-with-ripgrep.md
Normal file
40
unix/negative-look-ahead-search-with-ripgrep.md
Normal file
@@ -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!
|
||||
Reference in New Issue
Block a user