All, I have a series of SQL statements seperated in a string by ‘GO’ (case insensitive, so ‘Go’, ‘go’ et al. are also valid). If I have a statement like
string str = "INSERT INTO People \r\n" +
"VALUES (3, 'Gandalf', 'The Gray'); \r\nGO\r\n" +
"INSERT INTO People \r\n" +
"VALUES (4, 'Legolas', 'Camus'); \r\nGO";
I want to avoid a match on the ‘go’ in ‘Legolas’. To do this I devised a regular expression in LINQPad with both negative lookahead and positive lookbehind, and have
string str = "INSERT INTO People \r\n" +
"VALUES (3, 'Gandalf', 'The Gray'); \r\nGO\r\n" +
"INSERT INTO People \r\n" +
"VALUES (4, 'Legolas', 'Camus'); \r\nGO";
Regex regGo = new Regex(@"(?<=\b)(?i)GO(?!\w+)");
MatchCollection matches = regGo.Matches(str);
foreach (Match m in matches)
{
m.Dump();
}
Which does what I want, namely, it only matches a ‘GO’ (of any case) when it is a new ‘GO’ and not followed by anything. This works in LINQPad – i.e. returns the required result; but not in my C# app.
Why the descrepancy and what am I doing wrong?
Thanks for your time.
I was able to get your example to work in both LinqPad and a Console app.
A suggestion to simplify your regex and avoid using look behind and look ahead would be to use word boundaries like this:
This will accomplish the same result and be a little more readable.