I need to capture only the
where clause of several queries to analyze the filters used. For example:
select "DIM_1"."col1", "DIM_2"."col2" , "DIM_3"."col3" , "DIM_4"."col4" , "FAT_1"."col5" from "FAT_1", "DIM_1", "DIM_2", "DIM_3", "DIM_4" where "DIM_1"."col1" IS NOT NULL AND "DIM_2"."col2" LIKE ('SUCCESS') AND "DIM_3"."col3" BETWEEN 20161213 AND 20161222 AND "DIM_4"."col4" > 0
I created a list with SQL, and then I tried to apply regular expressions to extract the where part, but without success, here's what I tried:
`for line in sql:` `if re.search(r'[where]\W',line):` `where.append(line)`
Unfortunately I couldn't extract only the where part, can you tell me what mistake I made and how to fix it?
I believe that what you want is to get the
First let's check what can come after the
where post-following commands
According to postgres .
I only sell the most common
- GROUP BY
- ORDER BY
- "NADA" – Because it can be just the
wherewithout any subsequent command.
- pattern :
(?<=where)(.*?)((ORDER BY|GROUP BY|HAVING|LIMIT|OFFSET|$).*)
- flags :
(?<=where)– ensures that what we are searching for comes after
(.*?)– everything that follows will be the clauses.
((ORDER BY|GROUP BY|HAVING|LIMIT|OFFSET|$).*)– guarantees that it will end at one of the commands or at the end(
- Flag :
s– says that the
.(dot) must include
\nin the search.
- Flag :
i– case-insensitive – allows you to search for uppercase or lowercase.