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

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

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. Brian Candy
    Replies:
    2
    Views:
    1,449
    Janaka
    Feb 18, 2004
  2. ecoolone
    Replies:
    0
    Views:
    947
    ecoolone
    Jan 3, 2008
  3. Greg Hurrell
    Replies:
    4
    Views:
    336
    James Edward Gray II
    Feb 14, 2007
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    748
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    671
    7stud --
    Aug 21, 2009
  6. Neil Shadrach

    regexp to sql wildcard conversion

    Neil Shadrach, Jun 27, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    845
    pkent
    Jun 27, 2005
  7. Uldis  Bojars
    Replies:
    2
    Views:
    336
    Janwillem Borleffs
    Dec 17, 2006
  8. Matìj Cepl

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

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