Search code examples
oracle-databasereplaceplsqlparametersregexp-replace

How to use REGEXP_REPLACE to remove parameters from function?


Image Here

I have a column that stores C# codes, how do I remove the last 3 parameters of the "FunctionA"? Note that the column contains multiple functions, but I only need to replace "FunctionA" using PL/SQL, I know REGEXP_REPLACE might do the trick, but I can't seem to find a way to match/replace it.

Before:

Test=FunctionA(varID, 1234,"", A.B,"","","last");

Test=FunctionA(varID, 9876,"", C.D);

Test=FunctionB(varID, 5555,"", E.F,"","","last");

After:

Test=FunctionA(varID, 1234,"", A.B);

Test=FunctionA(varID, 9876,"", C.D);<- should not affect this

Test=FunctionB(varID, 5555,"", E.F,"","","last");<- should not affect this


Solution

  • Try finding this pattern:

    (,[^,]*,[^,]*,[^,]*\);)$
    

    And then replace with just );. Here is a sample query:

    SELECT
        REGEXP_REPLACE ('Test=FunctionA(varID, 1234,"", A.B,"","","last");',
            '(,[^,]*,[^,]*,[^,]*\);)$', ');') AS output
    FROM dual
    WHERE col LIKE 'Test=FunctionA(%'
    
    Test=FunctionA(varID, 1234,"", A.B);
    

    Demo

    Edit: I added a WHERE clause which checks the function name.