Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
(?<=FROM|JOIN|OJ)[\s\(]+((((?!SELECT\b)`?[\w_]+`?)(\s*\)?,\s*)*)+)
/
gi

Description

Finds table names in table list and subqueries. Table names are in group #1 of all matches One note. In case of table list (like SELECT * FROM table1, table2, etc) group#1 will contain all tables string (like "table1, table2, etc"). In case of subquery in a table list group#1 may contain also other symbols besides table names and commas (most likely - "(" ). So it's necessary to do additional handing like splitting the string.

Submitted by Mykhaylo Khodorev (mykhaylo.khodorev@gmail.com) - 9 years ago