需要用reg exp解析sql

问题描述 投票:0回答:1

我需要从 pl sql 查询中删除与“where”块中的日期相关的任何条件。我使用 python re 库和 re.sub() 函数。 Ass reexp 模式,我写了这个正则表达式:

((?<=where)|(?<=and))[^()]*?\w*date\w*.*?((and)|(?=order|where|left|right|inner|join|full))

这是一个工作不正确的例子: https://regex101.com/r/HMAWI8/1

正如您可以检查的那样,对于此模式部分:

 a.f_funid = b.f_funid and
,出于某种原因被选择作为较大匹配的一部分
a.f_funid = b.f_funid and a.begin_date <= to_date('11.11.1111 01:00:50','dd.mm.yyyy hh24:mi:ss') and
,尽管在我看来,
and
部分应该将其分开。我不明白为什么它忽略了第一个
and
部分。

结果我想得到这一行:

select any from (select any from some where a.f_funid = b.f_funid) r left join ods.account_h a on r.f_account_id = a.f_account_id where r.f_picture in (select r.f_picture from ods.RELATION_h r, ods.CALENDAT_h c, ods.REV_h rev where c.F_REVALUATIONID = rev.F_REVALUATIONID) and lower(f_name) = 'smth') and  union all 

我尝试使用负环视而不是

.*?
[^()]
部分,但似乎这是解决此问题的错误方法。我现在正处于死胡同。

python sql regex plsql python-re
1个回答
0
投票

正如 CharlieFace 所提到的“SQL 是一种非常规语法”。
正如您在问题标题中所写:您需要解析 SQL。
另外,正如您提到的 python 作为问题的标签:

您是否尝试使用 SQLparse python 包
您还可以在这个用 Python 解析 SQL

的答案中找到解析 SQL 的其他选项
© www.soinside.com 2019 - 2024. All rights reserved.