I am writing a c# class library that will allow me to scan an SQL server query and extract objects from the query into their correct groupings, for example:
SELECT * FROM "My Server"."Northwind"."dbo"."Product Sales for 1997" Group By CategoryID
This regex will will match the above string and group "My Server", "Northwind", "dbo" and "Product Sales for 1997" into four groups, which is what I want.
(?i)\bFROM\b\s+[\["]([^\]"]*)[\]"].{1}[\["]([^\]"]*)[\]"].{1}[\["]([^\]"]*)[\]"].{1}[\["]([^\]"]*)[\]"].{1}
What I am looking for is a single regex expression that can capture the server name, database name, schema name and object name for any of the following combinations (this is not an exhaustive list by any means):
SELECT * FROM dbo."Product Sales for 1997" // should return groups 2 & 3
SELECT * FROM Northwind."My Schema"."My view or table function" // should return groups 1, 2 & 3
SELECT * FROM "My view or table function" // should return group 3
SELECT * FROM dbo."My View 1" AS V1 JOIN "My View 1" AS V2 ON V1.ID = V2 // should return groups 2 & 3
In other words I want to capture the various components into the following groups:
Group 0 --> Server Name
Group 1 --> Database Name
Group 2 --> Schema
Group 3 --> Object Name
I am trying to avoid creating multiple regex expressions to handle every possible combination to avoid my class library becoming too large and complex, but as a regex n00b it's proving a bit difficult.