package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)(?:(?:CREATE(?:(?:\s*\/\*.*\*\/\s*?)*\s+OR(?:\s*\/\*.*\*\/\s*?)*\s+REPLACE)?|DROP)(?:\s*\/\*.*\*\/\s*?)*\s+TABLE(?:(?:\s*\/\*.*\*\/\s*?)*\s+IF(?:\s*\/\*.*\*\/\s*?)*\s+EXISTS)?|UPDATE|DELETE|INSERT(?:\s*\/\*.*\*\/\s*?)*\s+INTO)(?:\s*\/\*.*\*\/\s*?)*\s+([^\s\/*]+)`)
var str = `CREATE TABLE tbl1 ...
CREATE OR REPLACE TABLE tbl1 ...
DROP TABLE IF EXISTS tbl1; CREATE TABLE tbl1 ...
INSERT /*some comment*/ INTO tbl2 ...
INSERT /*some comment*/ INTO tbl2 ...
UPDATE tbl3 SET col1 = ...
/*some garbage comments*/ UPDATE tbl3 SET col1 = ...
DELETE tbl4 ...
#1 some optional statements like drop table CREATE /*some comments*/ TABLE table_name everything else
#1 some optional statements like drop table CREATE /*some comments*/ OR REPLACE TABLE table_name everything else
#2 some optional statements like drop table INSERT /*some comments*/ INTO /*some comments*/ table_name
#3 some optional statements like drop table UPDATE /*some comments*/ table_name everything else`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/