Using Regex to extract table names from a file containing SQL queries

It depends on structure of your file. Try to use this:

(?<=from|join)(\s+\w+\b)

Also turn on options Multiline if your not split your file in array or smth else with singleline string members. Also try to turn on IgnorCase option.


I'd use:

r = new Regex("(from|join)\s+(?<table>\S+)", RegexOptions.IgnoreCase);

once you have the Match object "m", you'll have the table name with

m.Groups["table"].Value

example:

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();
}

it will print: tb_table tb_table2

Tags:

C#

Regex