I would use:
r = new Regex("(from|join)\s+(?<table>\S+)", RegexOptions.IgnoreCase);
when you have a match object "m", you will have a 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
source share