(?m)\A(?=.*REGEX_TO_FIND)(?!.*MISSING_REGEX.*).*\z
The regex can get kinda tricky but it breaks down into two pieces.
- Find the matching term/phrase/word. This part isn't too tricky as this is what regex normally looks for.
- Finding the term not present. This is the tricky part, but it's possible.
I have an example HERE which shows how you want to find the word connectReadOnly
in the text, and fail to find disconnect
. Since the text contains connectReadOnly
it starts looking for the next piece, not finding disconnect
. Since disconnect
is in the text it fails on the entire string (what you will need for your entire file to match). If you play around with the second piece, the negation part (?!.*disconnect.*)
, you can set that as whatever regex you need. In my example I don't want to find disconnect
anywhere in my code :) You can easily replace that with your word to search on, or even a more complex regex to "not find".
The key is to use multi line mode, which is set using the beginning (?m)
and then using the start/end of string chars. Using ^
and $
to start/end a line, where \A
and \z
start and end a string, thus extending the match over the entire file.
EDIT: For the connectReadOnly
and disconnect
question use: (?m)\A(?=.*connectReadOnly)(?!.*disconnect.*).*\z
. The updated example can be found here.