replaceAll("^.*\\s+FROM\\s+(.+)\\s+WHERE\\s+.*$", "\\1")
As a quick first attempt. But I would really need to ask why. There may be better ways of doing what you want to do than pulling things out of an SQL statement.
Also, if the above works (haven't tested), it will only work if there are no subqueries. And if you need a String array (using Split(",")) then it will only work if you are not using aliases (which would require additional work).
So, I say again, what exactly. is it that you need, that caused you to want to start pulling things out of an SQL statement.
Edit: The string must also be uppercased beforehand, and it must include a where clause (if not the regex must be changed). All the more reason to answer my question.
> Here's my first try:
> > String[] split = "select * FROM a,b, c , d WHERE x =
> 1".split("(?i)(^.*from\\s*)|(\\s*,\\s*)|(\\s*where.*$)
> ");
>
> The first array entry is empty, ignore it.
Same problem, of course, with aliases and subqueries. ;-)
> > Same problem, of course, with aliases and
> subqueries. ;-)
>
> Indeed. Anonymous views too. I hope the OP uses an
> "easy" query ...
That's why I say he should describe what he needs, rather than descibing what he thinks he needs to do to satisfy those needs, which is what this question is. ;-)
Try this ugly monster:
String query = "select * from a,b where a.x=b.y";
query = query.toLowerCase().replaceAll(
"(?<=from\\s)(\\w+)\\s?,\\s?(\\w+)(?=\\s?)",
"schema.$1,schema.$2");
query = query.toLowerCase().replaceAll(
"(?<=where\\s)(\\w+)\\.(\\w+)\\s?\\=\\s?(\\w+)\\.(\\w+)(?=\\s?)",
"schema.$1.$2=schema.$3.$4");
> i know and so...?
So?
I just lost interest in this thread. You don't seem to be able to adjust the code already posted here into something you can use. I like helping but I don't feel like doing your work for you. If you have a specific question about a piece of Java code, feel free ask it here.
Good luck.