match tables name in select staement with RegExp

Hi allI need to get all tables in a sql select statement using RegExp.Anyone knows how?
[108 byte] By [gionnyDeepa] at [2007-11-27 7:47:55]
# 1

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.

masijade.a at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 2
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.
quittea at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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. ;-)

masijade.a at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 4
2nd try:String[] split = "select * FROM asds,bsdcf, c , dsdf WHERE x = 1".replaceAll("(?i)(^.*from\\s*)|(\\s*where.*$)", "").split("\\s*,\\s*");
quittea at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 5
> Same problem, of course, with aliases and subqueries. ;-)Indeed. Anonymous views too. I hope the OP uses an "easy" query ...
quittea at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 6
Refactored for alias support:/* your query */.replaceAll("(?i)(^.*from\\s*)|(\\s*where.*$)", "").split("(\\s+[^,]*)?,\\s*");
quittea at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 7

> > 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. ;-)

masijade.a at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 8
i choose yours.Thank u very much !!!
gionnyDeepa at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 9
and if i want to add some prefix before my matching elements as a schema ?
gionnyDeepa at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 10
I want to translate "select * from a,b where a.x=b.y" into "select * from schema.a,schema.b where schema.a.x=schema.b.y"
gionnyDeepa at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 11

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");

prometheuzza at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 12
the problem of this solution is that i do not know previously the select statement and how many tables i have in it.
gionnyDeepa at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 13
> the problem of this solution is that i do not know> previously the select statement and how many tables i> have in it.Then you cannot use it.
prometheuzza at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 14
i know and so...?
gionnyDeepa at 2007-7-12 19:28:59 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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.

prometheuzza at 2007-7-21 22:21:48 > top of Java-index,Java Essentials,Java Programming...
# 16
so what?
corlettka at 2007-7-21 22:21:48 > top of Java-index,Java Essentials,Java Programming...
# 17
and if i need all the columns of my sql statement now?
gionnyDeepa at 2007-7-21 22:21:48 > top of Java-index,Java Essentials,Java Programming...
# 18
> and if i need all the columns of my sql statement now?Then you're screwed since you cannot do your job properly. Anything else?
prometheuzza at 2007-7-21 22:21:48 > top of Java-index,Java Essentials,Java Programming...
# 19
Prometheuz:Do you have a regular expression that would allow me to select any column from any table within any schema in my RDBMS?Thanks and I look forward to your answer. ;-)
filestreama at 2007-7-21 22:21:48 > top of Java-index,Java Essentials,Java Programming...