SQL SELECT Statement
Match the column list and table name from a SQL SELECT ... FROM statement.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("SELECT\\s+(.+?)\\s+FROM\\s+([\\w.\"`\\[\\]]+)", "gis");
const input = "SELECT id, name FROM users";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
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+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("(?is)SELECT\\s+(.+?)\\s+FROM\\s+([\\w.\"`\\[\\]]+)")
input := `SELECT id, name FROM users`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
SELECT\s+(.+?)\s+FROM\s+([\w."`\[\]]+) (flags: gis)Raw source: SELECT\s+(.+?)\s+FROM\s+([\w."`\[\]]+)
How it works
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
—Common use cases
- •SQL parsing in lint / formatter tooling
- •Extracting referenced tables from migration files
- •Query observability — surfacing what tables a service hits
- •ORM-vs-raw-SQL audit scripts
Related patterns
Python Import Statement
Text ProcessingMatch Python `import x` and `from x import y` statements, capturing the module and target.
Terraform Resource Block Header
Text ProcessingMatch the opening line of a Terraform `resource "type" "name" {` block, capturing the resource type and local name.
GraphQL Operation Header
Text ProcessingMatch GraphQL operation headers — `query`, `mutation`, or `subscription` — capturing the operation name.
HCL / Terraform Variable Reference
Text ProcessingMatch Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.