Maybe try it this way
String data1="SELECT name, age FROM table1 whatever";
String data2="SELECT name, age FROM schema.table1 whatever";
Pattern p=Pattern.compile("from\s+(?:\w+\.)*(\w+)($|\s+[WHERE,JOIN,START\s+WITH,ORDER\s+BY,GROUP\s+BY])",Pattern.CASE_INSENSITIVE);
//test
Matcher m=p.matcher(data1);
while(m.find())
System.out.println(m.group(1));
m=p.matcher(data2);
while(m.find())
System.out.println(m.group(1));
output:
table1
table1
Edit
I just realized that part ($|\s+[WHERE,JOIN,START\s+WITH,ORDER\s+BY,GROUP\s+BY])
doesn't work as it should because in my input i placed "whatever" after table name and it was found anyway.
It doesn't work like you because you are using [WHERE,JOIN,START\s+WITH,ORDER\s+BY,GROUP\s+BY]
instead of (WHERE|JOIN|START\s+WITH|ORDER\s+BY|GROUP\s+BY)
. For example [abc]
is equal to (a|b|c) so it says regular expression engine to accept any character from that set, not a word abc
. Improve your pattern to something like
Pattern p=Pattern.compile("from\s+(?:\w+\.)*(\w+)(\s*$|\s+(WHERE|JOIN|START\s+WITH|ORDER\s+BY|GROUP\s+BY))",Pattern.CASE_INSENSITIVE);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…