SQL SELECT Statement in PY
Match the column list and table name from a SQL SELECT ... FROM statement.
Try it in the PY tester →Pattern
regexPY
SELECT\s+(.+?)\s+FROM\s+([\w."`\[\]]+) (flags: gis)Python (re) code
pyPython
import re
pattern = re.compile(r"SELECT\\s+(.+?)\\s+FROM\\s+([\\w.\"`\\[\\]]+)", re.IGNORECASE | re.DOTALL)
input_text = "SELECT id, name FROM users"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
How the pattern works
SELECT\s+ matches the keyword and required whitespace. (.+?) lazily captures the column list. \s+FROM\s+ matches the FROM keyword. ([\w."`\[\]]+) captures the table identifier including dots (db.schema.table), and the three quoting styles SQL dialects use: "double", `backtick`, [bracket]. Flags: g (global), i (case-insensitive SELECT/FROM), s (dotAll so columns can span newlines).
Examples
Input
SELECT id, name FROM usersMatches
SELECT id, name FROM users
Input
select * from `orders`Matches
select * from `orders`
Input
INSERT INTO logsNo match
—