Daniel
Daniel

Reputation: 163

Regexp recursive find/replace in Notepad++

I am wondering if it is possible to write a Regular Expression in Notepad++, allowing me to modify an SQL INSERT script into DELETE.

Below is an example of what I am trying to do.

Input:

INSERT INTO table1 (xxxx, yyyy, zzzz, ....) VALUES (blah1111, foo2222, 3333333, ....);

Output:

DELETE FROM 
    table1
WHERE
    AND xxxx = 'blah1111'
    AND yyyy = 'foo2222'
    AND zzzz = '3333333'
    AND ....;

I have tried to use recursion, but i don't know how to properly make references for each recursion step.

My actual RegEx:

Find script: ((\w+)(?R)?, )
Replace script: (?1) = 

Upvotes: 2

Views: 990

Answers (1)

Johannes Riecken
Johannes Riecken

Reputation: 2515

I don't know how to do write recursive regular expressions in Notepad++, but you can use macros to automate at least part of the conversion. First you can run the RegEx

Find what: insert into (\w+) (\([^)]*\)) values (\([^)]*\));

Replace with: \2\n\3\nDELETE FROM\n\1\nWHERE\n

to get the basic structure of the DELETE script with two buffer lines above. Then you click Macro -> Start Recording and press Ctrl-Home, right arrow, Ctrl-Shift-right arrow, Ctrl-x, delete, delete, Ctrl-End, Tab, Ctrl-V, =, ', to move the first table column's name in the right position and continue until you have the

WHERE
    AND xxxx = 'blah1111'

correctly. Then you click on Stop Recording and playback the macro until all the lines are generated correctly. You can also try to add my recorded macro to %APPDATA%\Notepad++\shortcuts.xml directly:

<Macro name="ReorderSQL" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2442" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2318" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2327" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam="=" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam=" " />
    <Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2306" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2300" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2442" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2177" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2180" wParam="0" lParam="0" sParam="" />
    <Action type="0" message="2318" wParam="0" lParam="0" sParam="" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
    <Action type="0" message="2179" wParam="0" lParam="0" sParam="" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam="&apos;" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam="&#x000D;" />
    <Action type="1" message="2170" wParam="0" lParam="0" sParam="&#x000A;" />
</Macro>

Upvotes: 1

Related Questions