mirror of
https://github.com/jbranchaud/til
synced 2026-01-18 22:48:02 +00:00
Add Use Negative Lookbehind Matching With ripgrep as a Unix TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1728 TILs and counting..._
|
||||
_1729 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1773,6 +1773,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Unrestrict Where ripgrep Searches](unix/unrestrict-where-ripgrep-searches.md)
|
||||
- [Update Package Versions Known By asdf Plugin](unix/update-package-versions-known-by-asdf-plugin.md)
|
||||
- [Use fzf To Change Directories](unix/use-fzf-to-change-directories.md)
|
||||
- [Use Negative Lookbehind Matching With ripgrep](unix/use-negative-lookbehind-matching-with-ripgrep.md)
|
||||
- [Use Regex Pattern Matching With Grep](unix/use-regex-pattern-matching-with-grep.md)
|
||||
- [View A Web Page In The Terminal](unix/view-a-web-page-in-the-terminal.md)
|
||||
- [View The Source For A Brew Formula](unix/view-the-source-for-a-brew-formula.md)
|
||||
|
||||
42
unix/use-negative-lookbehind-matching-with-ripgrep.md
Normal file
42
unix/use-negative-lookbehind-matching-with-ripgrep.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Use Negative Lookbehind Matching With ripgrep
|
||||
|
||||
The most straightforward way to use `ripgrep` is to hand it a pattern. It will
|
||||
take that pattern and move forward through each file trying to find matches.
|
||||
|
||||
```bash
|
||||
$ rg 'TwilioClient\.new'
|
||||
```
|
||||
|
||||
That will find all occurrences of `TwilioClient.new` in available project files.
|
||||
|
||||
What if that pattern is too permissive though? That is going to match on
|
||||
occurrences of `TwilioClient.new` as well as things like
|
||||
`LoggingTwilioClient.new`. If we want to exclude the latter, there are a few
|
||||
ways to do that. One of them being the use of [the _negative lookbehind_ regex
|
||||
feature](https://www.pcre.org/current/doc/html/pcre2syntax.html#SEC23) that is
|
||||
available with PCRE2 (Perl-Compatible Regular Expressions).
|
||||
|
||||
A _negative lookbehind_ is like a standard pattern. We look forward through the
|
||||
document for the base pattern (like `TwilioClient\.new`). However, once we find
|
||||
that match, we then look back at the previous characters and if they match our
|
||||
negative lookbehind pattern, then it is no longer a positive match.
|
||||
|
||||
We can use one of the following to forms to achieve this:
|
||||
|
||||
```
|
||||
(?<!...) )
|
||||
(*nlb:...) ) negative lookbehind
|
||||
(*negative_lookbehind:...) )
|
||||
```
|
||||
|
||||
For instance, here is what this looks like for our example:
|
||||
|
||||
```bash
|
||||
$ rg -P '(?<!Logging)TwilioClient\.new'
|
||||
```
|
||||
|
||||
Note: we have to use the `-P` flag to tell `ripgrep` that we are using PCRE2
|
||||
syntax. Otherwise, it assumes a simpler regex syntax that doesn't support
|
||||
_negative lookbehind_.
|
||||
|
||||
See `man rg` for more details.
|
||||
Reference in New Issue
Block a user