I am trying to retrieve the table from a query string (eg. 'select foo from bar limit 10
' should return 'bar
').
I believe '(?<=\bfrom\\s)(\\w+)
' is what I was looking for but it is unsupported by Go regexp package. (http://play.golang.org/p/MJ3DUk6uuC)
You still can detect 'from xxx
', without looking the lookahead syntax not supported by re2.
Since you would then capture 'from
', you need to remove it from the result.
See playground:
r := regexp.MustCompile("(?:\\bfrom\\s)(\\w+)")
res := r.FindAllString(strings.ToLower("select foo from bar limit 10"), 1)
if len(res) != 1 {
panic("unable")
}
i := strings.LastIndex(res[0], " ")
fmt.Println(res[0][i+1:], i)
Output:
bar 4