If you really want to use regular expressions, you can use negative lookbehind to ensure that your string is not preceded by Contract.Require
. However, there are many caveats in this solution (such as commen, et cetera), so it's probably not your best option. Anyway, here's a simple demonstration (you need to adapt it) of using something similar in Python:
import re
reg = re.compile('(?<!Contract\.Require\()"([^"\\\]|\\\.)*"')
tests = [ 'Contract.Require("test string")',
'OtherRequire("test string" + someVar)',
'String var = "testString";',
'String another = "test\\"quotestring"',
'String empty = ""' ]
for test in tests:
m = reg.search(test)
print test, "wasn't matched." if m == None else "matched " + m.group(0) + "."
Output:
Contract.Require("test string") wasn't matched.
OtherRequire("test string" + someVar) matched "test string".
String var = "testString" matched "testString".
String another = "test\"quotestring" matched "test\"quotestring".
String empty = "" matched "".
The lookbehind in the expression above is (?<!Contract\.Require\()
, and the regex to match string literals is "([^"\\\]|\\\.)*"
. This slightly more complicated regex for string literals is required in order to be able to match strings such as "quote\"quote".
Hope it helps.