Avatar of Russ Suter
Russ Suter

asked on 

Need a Regex to extract the correct FROM clause

I'm looking for a Regex that will allow me to select the appropriate FROM clause in a SQL SELECT statement. I'm looking for the FROM clause for the outermost SELECT statement only. The example below shows in bold what I mean. This query, although fairly crappy as a SQL query, fairly well demonstrates what I mean. You can see that there are 3 separate FROM clauses. I can't always rely upon it being first because of the inline query in the SELECT clause. I can't always rely on it being last because of the subquery in the WHERE clause. The one thing I can know for certain is that there will be a 1 to 1 relationship between the SELECT and FROM keywords. I need my Regex to EXACTLY and ONLY match the FROM keyword associated with the outermost query.

SELECT
      [InvoiceNumber],
      [Company],
      [DocumentType],
      [AccountNumber],
      [DateInvoiced],
      [DateDue],
      (SELECT SUM([Amount]) FROM [Invoices])
FROM
      [Invoices]
WHERE
      [InvoiceNumber] NOT IN
      (
      SELECT
            [InvoiceNumber]
      FROM
            [InvoiceLines]
      WHERE
            [TimeStamp] BETWEEN '1/1/2019' AND '12/31/2019'
      )
Regular Expressions.NET ProgrammingC#

Avatar of undefined
Last Comment
Shaun Vermaak

8/22/2022 - Mon