简体   繁体   中英

Insert text on specific positions with notepad++ python script plugin

I should preface that I am a complete Python Newbie, but i need to fulfill a task :(

I already installed the python script plugin and read how it works. But i have huge problems to understand what i have to programm/script.

I have got a textfile, that i open with notepad++ that contains many statements like:

Insert into XXX (XXX,XXX,XXX) Values (XXX, XXX, XXX);

I need to transform those statements into by using notepad++ python script

BEGIN
Insert into XXX (XXX,XXX,XXX) Values (XXX, XXX, XXX);
EXCEPTION
WHEN dup_val_on_index
THEN dbms_output.PUT_LINE('XXX');
END;
/

That means i have to put code in front of and after the Insert into Statement.

Is anybody able to help me? I may help by econmic calculations or something like that, but this task, is pretty hard for me :/

Thank you alot :-)

Assuming this is the particular python plugin you are using, you could use regex replace:

editor.pyreplace(r"^([Ii]nsert into.*? [Vv]alues.*?\;)", r"BEGIN\r\n\1\r\nEXCEPTION\r\nWHEN dup_val_on_index\r\nTHEN dbms_output.PUT_LINE('XXX');\r\nEND;/\r\n")

Note: I'm not sure whether the 'XXX' string in dbms_output.PUT_LINE('XXX') is supposed to be the table name or one of the value or the literal string 'XXX'. You could add another capturing group to the regex string to catch whichever one was intended if the literal string

You could also use a similar search/replace pattern without the python plugin. Notepad++ supports regex search and replace.

Edit: This should work with Notepad++'s built-in search & replace in Regex mode:

Find what: ([Ii]nsert into.*? [Vv]alues.*?\;)
Replace with: BEGIN\r\n\1\r\nEXCEPTION\r\nWHEN dup_val_on_index\r\nTHEN dbms_output.PUT_LINE\('XXX'\);\r\nEND;/\r\n

If you are looking for a Notepad++ based solution of find and replace you can try the following.
I am assuming that dbms_output.PUT_LINE('XXX') is for displaying the tablename(first xxx in your insert statement).

On the Find & Replace uncheck Match case and select Regular Expression with match Newline unchecked ( Notepad++ version > 6.0)

Find

Insert\s*into\s*([a-z0-9]*)(\s*)(.*)(\s*)values(\s*)(.*);

Replace

BEGIN\r\n\Insert into \1 \3 values \6;\r\nEXCEPTION\r\nWHEN dup_val_on_index\r\nTHEN dbms_output.PUT_LINE\(\'\1\'\);\r\nEND;

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM