I need to find all the strings in a set of c# files, unless they are inside a Contract.Require statement. There could be any amount of text in the string, so between the ” and “, and any amount of text between the string and the Contract.Require.
I have this:
/".+"/
which finds strings, but I need to refine my search.
Is this even possible?
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:Output:
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.