regexp format for SQL

E

ELI

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?
 
T

Tad J McClellan

ELI said:
#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;
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top