regexripgrep

Exclude match if string is prefaced with another string


I have a feeling this will get closed as a duplicate as it seems like this would be a common ask...but in my defense, I have searched SO as well as Google and could not find anything.

I'm trying to search SQL files using ripgrep, but I want to exclude matches that are part of a single line SQL comment.

Match examples:

AKA, search for foobar but ignore the match if -- occurs at any point before it on that line.

I have tried things like negative lookbehinds and other methods, but I can't seem to get them to work.

This answer seemed like it would get me there: https://stackoverflow.com/a/13873003/3474677

That answer says to use (?<!grad\()vec to find matches of vec that are not prefaced with grad(. So if I translate that to my use case, I would get (?<!--)foobar. This works...but only for excluding lines that contain --foobar it does not handle the other scenarios.

Worst case scenario, I can just pipe the results from ripgrep into another filter and exclude lines that match --.*foobar but I'm hoping to find an all-in-one solution.


Solution

  • According to the comments, using ripgrep and enable --pcre2 you can use:

    ^(?:(?!--).)*\bfoobar\b
    

    See a regex demo.