python中的正则表达式获取某些PLSQL语句

epfja78i  于 2023-05-02  发布在  Python
关注(0)|答案(2)|浏览(102)

我有这个代码,需要一个正则表达式来获得从PL SQL代码得到的约束和检查行
创建表格

"OWB_RUN"."WITHOUT_RESCHEDULING_OFFER"
   (    "RESCHEDULING_OFFER_ID" NUMBER NOT NULL ENABLE,
    "OFFER_DAY" DATE,
    "F_OFFER_MADE" NUMBER(1,0),
    "F_ACCEPTED" NUMBER(1,0),
    "CIF" VARCHAR2(8),
     CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N1" CHECK (cif IS NOT NULL
) ENABLE,
     CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N2" CHECK (offer_day IS NOT NULL
) ENABLE,
     CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N3" CHECK (f_accepted IS NOT NULL
) ENABLE,
     CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_C1" CHECK (f_accepted IN (0,1,2)
) ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS NOLOGGING
  STORAGE(INITIAL 1048576 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "APPL_WORK_TBL"
  PARALLEL

我想要这样的输出:

['CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N1" CHECK (cif IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N2" CHECK (offer_day IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N3" CHECK (f_accepted IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_C1" CHECK (f_accepted IN (0,1,2))']
qvtsj1bj

qvtsj1bj1#

假设text是存储输入的变量,你可以尝试:

import re

out = re.findall(r'CONSTRAINT "[^"]+" CHECK \([^)]+\)+',
                 ''.join(map(str.strip, text.splitlines())))

输出:

['CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N1" CHECK (cif IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N2" CHECK (offer_day IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_N3" CHECK (f_accepted IS NOT NULL)',
 'CONSTRAINT "FS_WITHOUT_RESCHED_OFFERS_C1" CHECK (f_accepted IN (0,1,2))']
aemubtdh

aemubtdh2#

下面的正则表达式就可以做到这一点:

import re

sql_script = """
 "OWB_RUN"."WITHOUT_RESCHEDULING_OFFER"
...
"""

constraint_pattern = r'CONSTRAINT\s+"[\w_]+"(?:\s+CHECK\s+\([^\)]+\))'
matches = re.findall(constraint_pattern, sql_script, re.IGNORECASE)
print(matches)

你可以在这里找到这个正则表达式的解释:https://regexr.com/7cm9j

相关问题