I need a RegEx to extract the table names and aliases from an expression like:
INNER JOIN dbo.table1 t ON t.x = table n.y
INNER JOIN table2 on table2.x = table n.y
INNER JOIN table3 t3 on t3.x = tablen.y
From the above query, I would want to capture:
(Table Name, Alias)
table1, t
table2, " " <-- No alias specified so blank or null
table3, t3
Note that there are the following ways to specify a table:
- with or without an owner such as dbo.table1
- with or without an alias
In any event, all I care about is the table name and the alias.
In case it makes a difference, the Query will be against a SQL Server database, and I will be using it inside a C# application.
I am familiar with iterating through the Matches of Regex Match Collection as well as the RegEx groups. I am struggling only with the proper RegEx expression to use.
Answer
You could use:
(?i)\bJOIN\s+([\w.]+)(?:\s+(?!ON\b)(\w+))?
But note that this could give invalid results, for example if you have quoted strings with matching content. In such cases you'd be better off using a proper SQL parser instead of regex.
No comments:
Post a Comment