Issue
Langaue(Python3.X, Re library)
I have a string as follows
import re
query_string = r'SELECT "a"."name", "a"."create_date", "a"."state", SUM("b"."cost") AS "amount", SUM("b"."cost") FILTER (WHERE "a"."state" = 'UNPAID') AS "paid", SUM("b"."cost") FILTER (WHERE "a"."state" = 'PAID') AS "unpaid" FROM "maintenance"'
I want to select "column names" i.e. "a"."name", "a"."create_date", "a"."state"
. from above string.
Which comes between "SELECT" and "SUM(.*)"
Any help appreciated.
I have tried below Regular Expression pattern
r'SELECT (.* ), [^(SUM(.* )]'
r'SELECT (.* ), SUM(.* )'
but both are not giving accurate result
Expected result:
"a"."name", "a"."create_date", "a"."state"(No comma at the endth)
Solution
You can use
(?:SELECT\s*)(.*?)(?:,\s*SUM.*)
to create a single capturing group.
The two (?:...)
make non-capturing groups.
The (.*?)
is a non-greedy group, which will stop before the FIRST "SUM" instead of the last one.
Answered By - Yunnosch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.