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

后端 未结 5 761
慢半拍i
慢半拍i 2021-01-05 11:28

I\'ve a text file containing large number of queries. I want to get all the distinct tables used in the entire file in all the queries. The table name can come after a \'fro

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-05 11:53

    I'd use:

    r = new Regex("(from|join)\s+(?\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+(?
    \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

    提交回复
    热议问题