Reputation: 15660
I have an SQL string that looks something like this:
SELECT
USER."ID", USER."NAME", USER."BIRTH",USER."GENDER",
PACKAGE."type"
PACKAGE."code"
FROM
"DBNAME"."USER" USER,
"DBNAME2"."PACKAGE" PACKAGE
WHERE
USER."PACKAGE_ID" = PACKAGE."ID"
ORDER BY
USER."NAME";
How should I write my regular expression in C# to extract all the column names between the SELECT and FROM keywords, and then the table names in the FROM clause?
The expected output should find these so that I can put them into List
to loop through:
ColumnsList:
USER."ID"
USER."NAME"
USER."BIRTH"
USER."GENDER"
PACKAGE."type"
PACKAGE."code"
TablesList:
"DBNAME"."USER" USER
"DBNAME2"."PACKAGE" PACKAGE
Upvotes: 1
Views: 4588
Reputation: 3188
Use this Regex will get the column and table name:
(?is)SELECT(.*?)(?<!\w*")FROM(?!\w*?")(.*?)(?=WHERE|ORDER|$)
Code Samples:
string sql=@"SELECT
USER.""ID"", USER.""NAME"", USER.""BIRTH"",USER.""GENDER"",
PACKAGE.""type""
PACKAGE.""code""
FROM
""DBNAME"".""USER"" USER,
""DBNAME2"".""PACKAGE"" PACKAGE
WHERE
USER.""PACKAGE_ID"" = PACKAGE.""ID""
ORDER BY
USER.""NAME"";";
var reg=new Regex(@"(?is)SELECT(.*?)(?<!\w*"")FROM(?!\w*?"")(.*?)(?=WHERE|ORDER|$)");
var colunms=reg.Match(sql).Groups[1].Value.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
var tables=reg.Match(sql).Groups[2].Value.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
Upvotes: 5