In the following code I need to get table names as tokens using SQL reserved words as separators. The tokens should contain the table name or the table name followed by a suffix.
For example, given
table1 t1 inner join table2 t2 outer join table3
The code should return three tokens:
Token 1: table1 t1
Token 2: table2 t2
Token 3: table3
This code instead uses the first reserved word as token, without discarding any other following reserved words:
String str = "table1 t1 inner join table2 t2 outer join table3";
String [] tokens = sql2.split("\\son\\s|\\sinner\\s|\\souter\\s|\\sjoin\\s");
for (int i = 0; i<tokens.length; i++)
System.out.println("Token "+(i+1)+":"+tokens[i]);
This returns:
Token 1:table1 t1
Token 2:join table2 t2
Token 3:join table3
What is the problem and how to make this work?