Search code examples
c#regext-sql

Using regular expressions to remove all the GOs in a sql script file


I'm trying to parse a sort of big SQL script containing commands like create functions and store procedures. I want to split the file in strings whenever I find a GO statement (I want to execute it using ordinary ado.net instead of SMO).

The problem is that I haven't find a suitable regular expression for that so far. Using a simple \bGO\b ignoring case will split it. But will also split all go(s) inside a comment like

-- this go will also be split

Using this expression ^\bGO\b[^--]$ almost solve my problem but I get some error when I have two consecutive GOs (that for whatever reason are there and are behind my domain).

end
go 
GO 

This is how the end of a SP creation looks like in my script.

I'm doing it in C#

Thanks a lot

** EDIT **

A co-worker came up with a solution that, for now, worked for all my scripts

^\s*go\s*\r\n


Solution

  • You can try this:

    (?i-msnx:\b(?<!-{2,}.*)go[^a-zA-Z])
    

    meaning, mach the string go if it is not preceded by 2 or more dashes followed by anything.

    This should do the trick!

    Edited to force checking only at word boundaries

    Edited to ignore 'go' followed by letter/digits (last attempt :)) and added link to regular expression tool

    PS: In case you haven't found this is a great resource about RE.

    PS2: This is a great tool for RE authoring/test