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

Discussion in 'Java' started by jimmy, May 23, 2008.

  1. jimmy

    jimmy Guest

    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!
    jimmy, May 23, 2008
    #1
    1. Advertising

  2. jimmy

    hiwa Guest

    On May 23, 11:53 am, jimmy <> 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 + "','";
    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);
    }
    }
    hiwa, May 23, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    476
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    629
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    336
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,700
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    302
    Chris
    Aug 11, 2008
Loading...

Share This Page