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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.