简体   繁体   English

使用notepad ++ python脚本插件在特定位置插入文本

[英]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 :( 我应该以我是一个完整的Python新手开头,但我需要完成一项任务:(

I already installed the python script plugin and read how it works. 我已经安装了python脚本插件并阅读了它的工作原理。 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: 我有一个文本文件,可以用notepad ++打开,其中包含许多语句,例如:

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

I need to transform those statements into by using notepad++ python script 我需要使用notepad ++ python脚本将这些语句转换为

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: 假设是您正在使用的特定python插件,则可以使用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'. 注意:我不确定dbms_output.PUT_LINE('XXX')'XXX'字符串应该是表名还是该值或文字字符串'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. 您也可以在没有python插件的情况下使用类似的搜索/替换模式。 Notepad++ supports regex search and replace. Notepad ++支持正则表达式搜索和替换。

Edit: This should work with Notepad++'s built-in search & replace in Regex mode: 编辑:这应该与Notepad ++的内置搜索和Regex模式下的替换一起使用:

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. 如果您正在寻找基于Notepad ++的查找和替换解决方案,则可以尝试以下方法。
I am assuming that dbms_output.PUT_LINE('XXX') is for displaying the tablename(first xxx in your insert statement). 我假设dbms_output.PUT_LINE('XXX')用于显示表名(insert语句中的第一个xxx)。

On the Find & Replace uncheck Match case and select Regular Expression with match Newline unchecked ( Notepad++ version > 6.0) 在“查找和替换”上,取消选中“ Match case然后选择match Newline未选中match Newline正则表达式”(Notepad ++版本> 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 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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