The best solution will depend on what you know about input. For example, if you are looking for things that are not enclosed in double quotes, does this mean that double quotes will always be correctly balanced? Can they be avoided with backslashes or by enclosing them in single quotes?
Assuming the simplest case is no nesting, no shielding, you can use this kind of:
preg_match('/THIS(?=(?:(?:[^"]*+"){2})*+[^"]*+\z)/')
After finding the target (THIS), lookahead basically calculates double quotes from this point to the end of the line. If there is an odd number, the match should have occurred inside a pair of double quotes, so it is invalid (lookahead failed).
As you have discovered, this problem is not suitable for regular expressions; that all proposed solutions depend on features that are not found in real regular expressions, such as group capture, search queries, reluctant and possessive quantifiers. I would not even try this without possessive quantifiers or atomic groups .
EDIT: To deploy this solution to allow for double quotes that can be escaped with backslashes, you just need to replace the parts of the regex that match "nothing that is double quote":
[^"]
with “anything that is not a quote or backslash, or backslash followed by anything”:
(?:[^"\\]|\\.)
Since backslash escape sequences are relatively rare, it is worth matching as many unescaped characters as possible while you are in this part of the regular expression:
(?:[^"\\]++|\\.)
Putting it all together, the regex becomes:
'/THIS\d+(?=(?:(?:(?:[^"\\]++|\\.)*+"){2})*+(?:[^"\\]++|\\.)*+$)/'
Applies to your test string:
'Match THIS1 and "NOT THIS2" but THIS3 and "NOT "THIS4" or NOT THIS5" ' + 'but \"THIS6\" is good and \\\\"NOT THIS7\\\\".'
... it must match 'THIS1' , 'THIS3' , 'THIS4' and 'THIS6' .