Eärion Eärion - 1 year ago 125
Java Question

get table name from query using a regex

I'm looking into finding a Pattern in order to get the table name for this type of SQL query;

INSERT INTO table(uuid,type) VALUES (?,?)


Here I want to get "table"

I have a pattern but it's working only if there is no brackets like
INSERT INTO table VALUES (?,?)


[from|into|update]\s+(?:\w+.)(\w+)(\s$|\s+(WHERE))


Thanks

NB : it's not the same pattern than in get table name from query

Answer Source

This will never be a very fool proof way of extracting table names from SQL. But here is a solution to the problem using regex like you need. You've specified the WHERE part in the provided expression but is it really necessary if you just need the name?

(?is)\b(?:from|into|update)\s+(\w+)

Note that there are many ways a SQL statement can be formatted and it's very unlikely that anyone can come up with an expression that can parse SQL in the way you need.

Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download