tags:

views:

89

answers:

1

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?

+1  A: 

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.

JG