Hi,Could someone help write a regex parsing sql str for me:)

J

jimmy

the sql :$B!!!!(Bselect * from (select * from tbl) a where c1 = 1 order
by c2 desc, c3 asc

I want to parse the 'order by' clause to get the column-order pair,
the result would be looks like:


['c2','c3'] ['desc','asc']


Greatly appreciated!
 
H

hiwa

the sql :$B!!!!(Bselect * from (select * from tbl) a where c1 = 1 order
by c2 desc, c3 asc

I want to parse the 'order by' clause to get the column-order pair,
the result would be looks like:

['c2','c3'] ['desc','asc']

Greatly appreciated!
public class Jimmy{
public static void main(String[] args){
String qry = "select * from (select * from tbl) a where c1 = 1
order by c2 desc, c3 asc ";
String lead = "^.+order\\s+by\\s+";
String pairs = qry.replaceAll(lead, "");
pairs = pairs.trim();
String[] elements = pairs.split(",?\\s+");

String c;
String o;
c = o = "['";
for (int i = 0; i < (elements.length - 1); i += 2){
c += elements + "','";
o += elements[i + 1] + "','";
}
int ic = c.lastIndexOf(",'");
int io = o.lastIndexOf(",'");
c = c.substring(0, ic) + "]"; // trim the last ",'" and add a ']'
o = o.substring(0, io) + "]";

System.out.println(c + " " + o);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top