tags:

views:

41

answers:

3

I am trying to jot down regex to find where I am using ltrim rtrim in where clause in stored procedures.

the regex should match stuff like:

RTRIM(LTRIM(PGM_TYPE_CD))= 'P'))

RTRIM(LTRIM(PGM_TYPE_CD))='P'))

RTRIM(LTRIM(PGM_TYPE_CD)) = 'P'))

RTRIM(LTRIM(PGM_TYPE_CD))= P

RTRIM(LTRIM(PGM_TYPE_CD))= somethingelse))

etc...

I am trying something like...

.TRIM.*\)\s+
A: 

This what you want:

[LR]TRIM\([RL]TRIM\([^)]+\)\)\s*=\s*[^)]+\)*

?

What's that doing is saying:

[LR]    # Match single char, either "L" or "R"
TRIM    # Match text "TRIM"
\(      # Match an open parenthesis
[RL]    # Match single char, either "R" or "L" (same as [LR], but easier to see intent)
TRIM    # Match text "TRIM"
\(      # Match an open parenthesis   
[^)]+   # Match one or more of anything that isn't closing parenthesis
\)\)    # Match two closing parentheses
\s*     # Zero or more whitespace characters
=       # Match "="
\s*     # Again, optional whitespace (not req unless next bit is captured)
[^)]+   # Match one or more of anything that isn't closing parenthesis
\)*     # Match zero or more closing parentheses. 


If this is automated and you want to know which variables are in it, you can wrap parentheses around the relevant parts:

[LR]TRIM\([RL]TRIM\(([^)]+)\)\)\s*=\s*([^)]+)\)*

Which will give you the first and second variables in groups 1 and 2 (either \1 and \2 or $1 and $2 depending on regex used).

Peter Boughton
thats exactly what I was looking for. I really want to make this automated and find the table name as well!! but I think that will be a long shot as I go through the files i'll have to read backwards and stuff. So I am going to run a query on oracle after I have the column name and find out what the table name is. thanks for help
jason
Yeah, depends if there's an easy way to reliably identify all query blocks, then exclude ones that don't match the above - I'm not familiar with Oracle SP syntax, but I'd guess its getting more into SQL parser territory for that sort of stuff.
Peter Boughton
+1  A: 

[RL]TRIM\s*\( Will look for R or L followed by TRIM, any number of whitespace, and then a (

gnarf
this is also select stuff like `select RTRIM(LRTRIM(....` I only want stuff there I am using rtrim ltrim in `WHERE` clause
jason
I modified yours to be `[RL]TRIM\s*\(.*\s*=` I thin kthis will suffice
jason
A: 

How about something like this:

.*[RL]TRIM\s*\(\s*[RL]TRIM\s*\([^\)]*)\)\s*\)\s*=\s*(.*)

This will capture the inside of the trim and the right side of the = in groups 1 and 2, and should handle all whitespace in all relevant areas.

jdmichal