tags:

views:

457

answers:

4

I am using Regex.Split to split a SQL script on the keyword "GO". My problem here is that I cannot seem to get my head around how to get the Regex to do the split. My regex expression also splits on "GO" even if it's in a SQL statement like:

Insert into  x(a,b) values(‘please go get some text’,’abc’)

But I only want it to split on the keyword "GO". Any suggestions?

EDIT: I am using c#. at the moment my regex is simply:

  foreach (string batch in Regex.Split(script, "\\bGO\\b", RegexOptions.IgnoreCase))
  {
    yield return batch;
  }
+4  A: 

This is pretty much impossible without implementing a complete SQL parser (which you probably do not want to do), in the really correct way.

An alternative would be to resort to some hacks (i.e. ignore sections of the text that are within quotes), but this will still not work if your SQL contains the text 'GO' at some other place, e.g. 'SELECT * FROM GO'.

Martin Probst
+1, regex does not have the power to parse SQL, period. You may need to rethink your requirements. Writing a full SQL parser is hugely non-trivial.
bobince
@bobince: "hugely non-trivial"... oxymoron ? ;-)
Cerebrus
Err... how would that be an oxymoron? trivial would be small, non-trivial is therefore large... if anything it's redundant.
Telos
+3  A: 

You could search for "go" on a line by itself. Not guaranteed to always work, but more likely to work.

Joel Potter
Nah, I tried this and it matches "go" in a normal statement as well
Draco
Draco, your regex is wrong then
James L
+3  A: 

Split on GOs on a line by themselves, like:

foreach (string batch in Regex.Split(script, "^GO$\\n", RegexOptions.IgnoreCase | RegexOptions.Multiline))
{
    yield return batch;
}

Depending on where your script is coming from you may need to change that to "^GO$\\r\\n"

Gordon Bell
That regex has got the anchors mixed up. Also, make sure you set multiline mode: "(?m)^GO$"
Alan Moore
+2  A: 

You could try something like

/;\s*GO\s*;/i

With that you'll be covering every GO sentence, independently if they are in just one line or not (i.e. semi-colons in other lines).

If you're using queries for further execution, the you might want to add the semi-colon back to each query.

Be warned that if an occurrence of "; GO;" happens inside an insert string, then there's no way to achieve your goal without a proper SQL parser.

Seb