It is only a very simple SQL parsing process, no HAVING, ORDER BY or GROUP BY would be in the statement that needs to be parsed, but something about matching an asterisk in SELECT part won't work (e.g. SELECT * .......)
Here's the code part:
String span="SELECT * FROM table1 t, table2";
span=span.toUpperCase();
System.out.println(span);
// Segments
String column="(\\w+\\s*(\\w+\\s*){0,1})";
String columns=column+"(,\\s*"+column+")*";
String ownerenable="((\\w+\\.){0,1}\\w+\\s*(\\w+\\s*){0,1})";
String ownerenables=ownerenable+"(,\\s*"+ownerenable+")*";
String from="FROM\\s+"+columns;
String condition="(\\w+\\.){0,1}\\w+\\s*(=|<|>|LIKE|IS)\\s*'?(\\w+\\.){0,1}[\\w%]+'?";
String conditions=condition+"(\\s+(AND|OR)\\s*"+condition+"\\s*)*";
String where="(WHERE\\s+"+conditions+"){0,1}";
String pattern = "SELECT\\s+(\\*|" + ownerenables + "\\s+" + from + ")\\s+" + where + "\\s*" + "(;){0,1}";
System.out.println(pattern);
System.out.println(span.matches(pattern));
Now if I set span to something like
"SELECT t.id, product p FROM table1 t, table2 WHERE t.id=10;"
The match result would be true
================================
But if I set it to something like this:
"SELECT * FROM table1 t, table2 WHERE t.id=10;"
(It's an asterisk I'm using for the SELECT)
It won't work in this case
Am I doing anything wrong about that asterisk?
*is a meta-character it should be backslashed "\"* . It will match*. You need to add it to yourownerenable. However it will be quite tricky to do it with regex