正则表达式在C#解析SQL查询 [英] RegEx for parsing SQL query in C#

查看:973
本文介绍了正则表达式在C#解析SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写正则表达式在C#来解析SQL加入的是作为一个查询字符串。有人可以帮我,因为我在这个新的。非常感谢

I need to write RegEx in C# to parse SQL join query which is given as a string. Can somebody please help me because I'm new at this. Thanks a lot

这是我的问题:

string query = @"SELECT table1.column1, table1.column2, table2.coulmn1, table2.column2
                 FROM table1 INNER JOIN table2 ON table1.column5 = table2.column5";



是我真正需要的是把所有重要的数据到不同的变量,就像这样:

what I actually need is to put all important data into separate variables, like this:

string class1 = table1    
string class2 = table2    
string joinForeignKey1 = table1.column5    
string joinForeignKey2 = table2.column5    
List<string> attributes1 = table1.column1, table1.column2    
List<string> attributes2 = table2.column1, table2.column2



//注释

//COMMENT

我意识到,我已经在SQL查询错误所以会有ON子句。

I realized that I have made a mistake in sql query so there will be an ON clause.

我可以强制用户向我提供正确的语法,这样就没有问题了。

I can force a user to provide me with the correct syntax, so that will be no problem.

我没有提到的一点是,可以有多个JOIN ON子句(多连接)。

The thing I haven't mentioned is that there can be more than one JOIN ON clause (multiple joins).

非常感谢,我会感激任何帮助。

Thanks a lot and I will appreciative any given help.

推荐答案

从评论把该结束了,因为我觉得这里的正确答案:

Pulling this over from comments, since I think it's the right answer here:

SQL是东西名单上的#3你不应该试图解析用一个正则表达式,仅仅落后的 HTML 流行性腮腺炎。使用方言特有的,专用的SQL语法分析器,而不是一个正则表达式。

SQL is #3 on the list of Stuff You Should Not Try To Parse With A Regex, just behind HTML and MUMPS. Use a dialect-specific, dedicated SQL parser, not a regex.

这篇关于正则表达式在C#解析SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆