<p>I am not that hot at regular expressions and it has made my little mind melt some what.</p>

<p>I am trying to find all the tables names in a query.  So say I have the query:</p>

<pre><code>SELECT one, two, three FROM table1, table2 WHERE X=Y
</code></pre>

<p>I would like to pull out "table1, table2" or "table1" and "table2"</p>

<p>But what if there is no where statement.  It could be the end of the file, or there could be a group by or an order by etc.  I know "most" of the time this will not be an issue but I don't like the idea of coding for "most" situations and knowing I have left a hole that could cause things to go wrong later.</p>

<p>Is this a doable Regex expression?  Am I being a Regex pleb?</p>

<p>(P.S. this will be done in C# but presume that doesn't matter much).</p>

It's definitely not easy.
Consider subqueries.
```
```
select
  *
from
  A
  join (
    select
       top 5 *
    from
      B)
    on B.ID = A.ID
where
  A.ID in (
    select
      ID
    from
      C
    where C.DOB = A.DOB)
```
```
There are three tables used in this query.