You need to pull out the names of the main tables. Select ... From TableName There are four types of queries:

 1. Select ... From TableName 2. Select ... From Only TableName 3. Select ... From TableName1 Union Select ... From TableName2 4. Select ..., Exists(SELECT true FROM _tName WHERE ...) From TableName 

Wrote a regular season: (with gims test parameters here ) (scary queries with multiple line breaks, tabs and spaces, so I inserted \s\t\r\n everywhere)

 (?:[\s\t\r\n]*select.+?from[\s\t\r\n]*(?:only|)[\s\t\r\n]*)(\S*) 

Breaking through on the 4th example, how to take into account subqueries? For example, to select only Select..From between which there is not one more Select . If you just make greedy, then not all tables are found when Union

  • remove before processing everything exists \ (. +? \) is not an option? - nörbörnën
  • What exactly do you want to choose? What select? Which one in the subquery? Your regular expression does this: regex101.com/r/pK5xW3/1 - ReinRaus
  • @ReinRaus, in the fourth version does not correctly, because that I am not interested in the subquery, but it does not find the main request, the only shortcoming is Isaev
  • @norbornen, option, but I don’t really like it, I would like not to fence, but to solve everything in one regular season - Isaev
  • one
    @Isaev: Oh. Something seems to me, "mini" will not work. If there is a recursion, you have to write a full-fledged parser. - VladD 4:05 pm

1 answer 1

Solved by banal recursion in regular expression.
Sorry for the strange spelling. Habit.

 (?P<onlyselect>\bselect\s*.*?\bfrom\s*+(?:only)?\s*+\S++){0}\bselect\s*(?:(?P>onlyselect)|.)*?\bfrom\s*+(?:only)?\s*+(\S++) # IGNORECASE DOTALL 

Incoming text:

 1. Select ... From TableName1 2. Select ... From Only TableName2 3. Select ... From TableName3 Union Select ... From TableName4 4. Select ..., Exists(SELECT true FROM TableName5 WHERE ...) From TableName6 

Result:

 MATCH 1 2. [20-30] `TableName1` MATCH 2 2. [56-66] `TableName2` MATCH 3 2. [87-97] `TableName3` MATCH 4 2. [126-136] `TableName4` MATCH 5 2. [212-222] `TableName6` 

https://regex101.com/r/pK5xW3/3
PS Regular expression does not take into account comments, CDATA and other delights of SQL queries. It simply solves the problem posed in the question.

  • What you need! Thanks) And I will go to study banal recursion in regular expressions, have not heard about it, unfortunately - Isaev
  • 2
    J. Friedl. "Regular Expressions". The best there is on the topic. - ReinRaus