Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hi,Could someone help write a regex parsing sql str for me:)

Reply
Thread Tools

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

 
 
jimmy
Guest
Posts: n/a
 
      05-23-2008

the sql :  select * 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!

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      05-23-2008
On May 23, 11:53 am, jimmy <(E-Mail Removed)> wrote:
> the sql :  select * 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[i] + "','";
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);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if len(str(a)) == len(str(r)) and isMult(a, r): faster if isMult isslow? maestro Python 1 08-11-2008 01:17 PM
str.equals(null) or str==null ? Stefan Ram Java 21 08-03-2006 06:43 PM
It is fun.the result of str.lower(str()) Sullivan WxPyQtKinter Python 5 03-09-2006 08:09 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM



Advertisments