code_junkie

Most effective way to grab a table name in the following string

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. string q = @"SELECT [t0].[Id], [t0].[CODE] AS [arg0], [t0].[DESC] AS [arg1] FROM [SchemaName].[TableName] AS [t0] WHERE ([t0].[Id] <> @p0)";
  2. int fromIndex = q.IndexOf("FROM")+5;
  3. int asIndex = q.IndexOf("AS",fromIndex);
  4. q = q.Substring(fromIndex, asIndex - fromIndex);
  5.  
  6. string data = "SELECT [t0].[Id], [t0].[CODE] AS [arg0], [t0].[DESC] AS [arg1] FROM [SchemaName].[TableName] AS [t0] WHERE ([t0].[Id] <> @p0)";
  7. Regex re = new Regex(@"FROM (([w+].?){2}) AS");
  8. Match m = re.Match(data);
  9. if (m.Success){ Console.WriteLine(m.Groups[1]); }
  10.  
  11. string data = "SELECT [t0].[Id], [t0].[CODE] AS [arg0], [t0].[DESC] AS [arg1] FROM [SchemaName].[TableName] AS [t0] WHERE ([t0].[Id] <> @p0)";
  12. Regex re = new Regex(@"FROM [(w+)].[(w+)] AS");
  13. Match m = re.Match(data);
  14. if (m.Success){ Console.WriteLine("{0}.{1}", m.Groups[1], m.Groups[2]); }
Add Comment
Please, Sign In to add comment