regexp format for SQL

Discussion in 'Perl Misc' started by ELI, Jan 9, 2009.

  1. ELI

    ELI Guest

    I have a simple regexp that I want to use to format a SQL statement.

    #want to format the following:
    select
    --to_char(last_analyzed,'HH:MI:SS PM')
    *
    from
    user_indexes ui
    where
    UPPER(ui.index_name) = UPPER('fact_cnpt_pat_enct_idx')
    group by
    some column
    order by
    some other column

    #to be converted to the following format:

    select
    --to_char(last_analyzed,'HH:MI:SS PM')
    *
    from
    user_indexes ui
    where
    UPPER(ui.index_name) = UPPER('fact_cnpt_pat_enct_idx')
    group by
    some column
    order by
    some other column

    #basically this should take all of the NON-SQL structures (select,
    from, where, group, order) and indent them.
    # so far, the only thing I've managed to do is move the SQL
    structures, using the following REGEXP

    #this basically inserts a \t (tab) for every occurence
    \(^\(select\|from\|where\|order\|group\)\)
    \t\1


    #QUESTION -----------------
    # is there a way to say "give me everything (in a backreference) that
    is NOT found within the expression" -
    # basically it needs to be the opposite of it's current logic, but I'm
    not sure how to do that. Suggestions?
     
    ELI, Jan 9, 2009
    #1
    1. Advertising

  2. ELI <> wrote:

    > #want to format the following:
    > select
    > --to_char(last_analyzed,'HH:MI:SS PM')
    > *
    > from
    > user_indexes ui
    > where
    > UPPER(ui.index_name) = UPPER('fact_cnpt_pat_enct_idx')
    > group by
    > some column
    > order by
    > some other column
    >
    > #to be converted to the following format:
    >
    > select
    > --to_char(last_analyzed,'HH:MI:SS PM')
    > *
    > from
    > user_indexes ui
    > where
    > UPPER(ui.index_name) = UPPER('fact_cnpt_pat_enct_idx')
    > group by
    > some column
    > order by
    > some other column


    > #QUESTION -----------------
    > # is there a way to say "give me everything (in a backreference) that
    > is NOT found within the expression" -



    No.

    But there is a way to say "match where the following does not match".

    It is called a "negative look-ahead assertion" (perldoc perlre).


    > # basically it needs to be the opposite of it's current logic, but I'm
    > not sure how to do that. Suggestions?



    s/^(?!(select|from|where|order|group)\b)/\t/gm;


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 9, 2009
    #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. Greg Hurrell
    Replies:
    4
    Views:
    163
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    488
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    363
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    192
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    181
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page