C# 使用正则表达式从包含SQL查询的文件中提取表名
我有一个包含大量查询的文本文件。我希望在所有查询中获得整个文件中使用的所有不同表。表名可以位于“from”或“join”之后。如何通过进行正则表达式匹配来提取它们。有人能推荐一个正则表达式来获得匹配项吗?类似这样的内容可能是:C# 使用正则表达式从包含SQL查询的文件中提取表名,c#,regex,C#,Regex,我有一个包含大量查询的文本文件。我希望在所有查询中获得整个文件中使用的所有不同表。表名可以位于“from”或“join”之后。如何通过进行正则表达式匹配来提取它们。有人能推荐一个正则表达式来获得匹配项吗?类似这样的内容可能是: (from|join)\s(\w+) /(from|join)\s+(\w*\.)*(?<tablename>\w+)/ /(从|连接)\s+(\w*\)*(?\w+)/ 但它与转义表名不匹配,您需要使正则表达式求值不区分大小写。我会使用: r = ne
(from|join)\s(\w+)
/(from|join)\s+(\w*\.)*(?<tablename>\w+)/
/(从|连接)\s+(\w*\)*(?\w+)/
但它与转义表名不匹配,您需要使正则表达式求值不区分大小写。我会使用:
r = new Regex("(from|join)\s+(?<table>\S+)", RegexOptions.IgnoreCase);
例如:
string line = @"select * from tb_name join tb_name2 ON a=b WHERE x=y";
Regex r = new Regex(@"(from|join)\s+(?<table>\S+)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);
Match m = r.Match(line);
while (m.Success) {
Console.WriteLine (m.Groups["table"].Value);
m = m.NextMatch();
}
string line=@“从tb_name中选择*并在a=b(其中x=y)上加入tb_name 2”;
正则表达式r=新正则表达式(@“(from|join)\s+(?\s+),
RegexOptions.IgnoreCase | RegexOptions.Compiled);
匹配m=r.匹配(线);
while(m.Success){
Console.WriteLine(m.Groups[“table”].Value);
m=m.NextMatch();
}
它将打印:
tb_表
tb_表2取决于文件的结构。尝试使用以下方法:
(?<=from|join)(\s+\w+\b)
(?可以尝试此方法,但它不适用于所有类型的查询
public void Main()
{
// TODO: Add your code here
string Line = string.Empty;
using (StreamReader sr = new StreamReader(@"D:\ssis\queryfile.txt"))//reading the filename
{
var text = string.Empty;
do
{
// MessageBox.Show(Line);
text = Line = sr.ReadToEnd();// storing it in a variable by reading till end
MessageBox.Show(Line);
} while ((Line = sr.ReadLine()) != null);
var text1 = text.Replace("[", string.Empty).Replace("]", string.Empty);//replacing brackets with empty space
MessageBox.Show(text1);
Regex r = new Regex(@"(?<=from|join)\s+(?<table>\S+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);//regex for extracting the tablename after from and join
Match m = r.Match(text1);//creating match object
MessageBox.Show(m.Groups[1].Value);
var v = string.Empty;
while (m.Success)
{
v = m.Groups[0].Value;
m = m.NextMatch();
StreamWriter wr = new StreamWriter(@"D:\ssis\writefile.txt", true);// writing the match to the file
var text2 = v.Replace(".", " ,"); // replace the . with , seperated values
wr.WriteLine(text2);
sr.Close();
wr.Close();
}
}
}
public void Main()
{
//TODO:在此处添加代码
string Line=string.Empty;
使用(StreamReader sr=new StreamReader(@“D:\ssis\queryfile.txt”)//读取文件名
{
var text=string.Empty;
做
{
//MessageBox.Show(行);
text=Line=sr.ReadToEnd();//通过读取直到结束将其存储在变量中
MessageBox.Show(行);
}而((Line=sr.ReadLine())!=null);
var text1=text.Replace(“[”,string.Empty)。Replace(“]”,string.Empty);//用空格替换方括号
MessageBox.Show(text1);
Regex r=new Regex(@)(?不,只匹配“from”或“join table_name”,但不匹配“from table_name"。管道会分割整个模式,而不仅仅是第一部分。嗯。那么我可以用括号或其他什么东西将from | join括起来吗?对不起,它甚至没有返回一个匹配项。绝对错误。你找不到具有此类正则表达式的表名。你将匹配单词join和from。不仅是表名。我还没有测试它,但它应该匹配每个匹配项f'from'或'join',后跟至少一个空格,然后是一个或多个用点分隔的标识符。但是有一个错误;它应该以单词断言开头,否则它也会匹配类似于foobarfrom blah
。它不考虑转义,因为这是MySQL使用的DBMS特有的东西反引号,PostgreSQL使用双引号,T-SQL使用方括号。哦,显然,正则表达式无论如何都不是一种可靠的方法。如果你想要可靠,你需要一个完整的SQL解析器。我认为你必须将\s+移到正向lookbehind-1正则表达式中不是解决这个问题的正确方法。正如tdammers所说,SQL解析器f需要一些描述才能有效地解决这个问题。@El Ronnoco我要求使用正则表达式来解决问题,因此他提供了解决方案。我只需要一个快速而肮脏的解决方案,我就得到了。对于支持模式,我添加了一些符号:(?我认为如果字符串中有注释select,它将不起作用。
public void Main()
{
// TODO: Add your code here
string Line = string.Empty;
using (StreamReader sr = new StreamReader(@"D:\ssis\queryfile.txt"))//reading the filename
{
var text = string.Empty;
do
{
// MessageBox.Show(Line);
text = Line = sr.ReadToEnd();// storing it in a variable by reading till end
MessageBox.Show(Line);
} while ((Line = sr.ReadLine()) != null);
var text1 = text.Replace("[", string.Empty).Replace("]", string.Empty);//replacing brackets with empty space
MessageBox.Show(text1);
Regex r = new Regex(@"(?<=from|join)\s+(?<table>\S+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);//regex for extracting the tablename after from and join
Match m = r.Match(text1);//creating match object
MessageBox.Show(m.Groups[1].Value);
var v = string.Empty;
while (m.Success)
{
v = m.Groups[0].Value;
m = m.NextMatch();
StreamWriter wr = new StreamWriter(@"D:\ssis\writefile.txt", true);// writing the match to the file
var text2 = v.Replace(".", " ,"); // replace the . with , seperated values
wr.WriteLine(text2);
sr.Close();
wr.Close();
}
}
}