sqlregexnotepad++

regex to match at certain string and not followed by another string using notepad++


for following sample sql:

CREATE TABLE "abc01"."SLI_EDF_INSTR" 
   (    "EDF_INSTR_KEY" NUMBER(10,0) NOT NULL ENABLE, 
    "EDF_KEY" NUMBER(10,0) NOT NULL ENABLE, 
    "INSTR_KEY" NUMBER(10,0) NOT NULL ENABLE, 
    "LAST_REC_TXN_USER_ID" VARCHAR2(32 BYTE) NOT NULL ENABLE, 
    "LAST_REC_TXN_TYPE_CODE" VARCHAR2(1 BYTE) NOT NULL ENABLE, 
    "LAST_REC_TXN_DATE" TIMESTAMP (6) NOT NULL ENABLE, 
    "LAST_REC_TXN_USER_POST_NAME" VARCHAR2(60 BYTE) NOT NULL ENABLE);

   

CREATE OR REPLACE EDITIONABLE TRIGGER "abc01"."TRG_SLEI_BIU" 
BEFORE INSERT OR UPDATE ON SLI_EDF_INSTR
    FOR EACH ROW
        DECLARE
            post_name VARCHAR2(32);
        BEGIN
            IF :NEW.LAST_REC_TXN_USER_ID IS NULL THEN
                :NEW.LAST_REC_TXN_USER_ID := 'SYSTEM';
            END IF;

            IF :NEW.LAST_REC_TXN_DATE IS NULL OR UPDATING THEN
                :NEW.LAST_REC_TXN_DATE := SYSTIMESTAMP;
            END IF;

            IF :NEW.LAST_REC_TXN_USER_POST_NAME IS NULL THEN
                SELECT NVL(MAX(USER_POST_NAME), 'SYSTEM') INTO post_name FROM UAC_USER WHERE UPPER(TRIM(USER_ID)) = UPPER(TRIM(:NEW.LAST_REC_TXN_USER_ID));
                :NEW.LAST_REC_TXN_USER_POST_NAME := post_name;
            END IF;

            IF INSERTING THEN
                :NEW.LAST_REC_TXN_TYPE_CODE := 'I';
            ELSIF UPDATING THEN
                IF :NEW.LAST_REC_TXN_TYPE_CODE <> 'D' OR :NEW.LAST_REC_TXN_TYPE_CODE IS NULL THEN
                    :NEW.LAST_REC_TXN_TYPE_CODE := 'U';
                END IF;
            END IF;
        END;

I want to write a regex to match start from CREATE TABLE up to but not including "CREATE OR REPLACE...", once I see "CREATE OR REPLACE", then stop the matching and exclude "CREATE OR REPLACE" in the match.

the desired match is:

CREATE TABLE "abc01"."SLI_EDF_INSTR" 
       (    "EDF_INSTR_KEY" NUMBER(10,0) NOT NULL ENABLE, 
        "EDF_KEY" NUMBER(10,0) NOT NULL ENABLE, 
        "INSTR_KEY" NUMBER(10,0) NOT NULL ENABLE, 
        "LAST_REC_TXN_USER_ID" VARCHAR2(32 BYTE) NOT NULL ENABLE, 
        "LAST_REC_TXN_TYPE_CODE" VARCHAR2(1 BYTE) NOT NULL ENABLE, 
        "LAST_REC_TXN_DATE" TIMESTAMP (6) NOT NULL ENABLE, 
        "LAST_REC_TXN_USER_POST_NAME" VARCHAR2(60 BYTE) NOT NULL ENABLE);
    
             

How to do this?

In notepad++, I try to tick the checkbox ". matches newline", select "regular expression" then find

CREATE TABLE.+(?![CREATE OR REPLACE])

but it match all words.


Solution

  • This Regex will match anything Starting with CREATE, using negative lookahead to not match the OR REPLACE, picking up anything till the next semicolon.

    CREATE(?! OR REPLACE)[\S\s]*?;
    

    Update: An alternative and more efficient approach would be:

    CREATE(?! OR REPLACE)[^;]*;
    

    In Notepad++ use search and mark and the regex and see for yourself.

    Got to Find...

    Use Mark...

    Result:

    End Result