使用PythonScript Notepad++ 插件进行 Python 正则表达式搜索和替换。功能
见这里
Editor.pyreplace(search, replace[, count[, flags[, startLine[, endLine]]]])
和
Editor.pymlreplace(search, replace[, count[, flags[, startPosition[, endPosition]]]])
这是一个使用 python 正则表达式搜索和替换函数 editor.pyreplace() 的简单程序,
我在其中留下了很多调试代码,因此您可以查看函数运行过程中发生的情况。
# $Revision: 1.3 $
# $Author: dot $
# $Date: 2012/04/19 00:03:26 $
from Npp import *
import re, string
expression = notepad.prompt(
"Enter the search string on the first line, followed by Ctrl+Enter, \n" +
"followed by the replace string on second line",
"RegEx and Search/Replace" ,
"")
debug = True
#debug = False
if debug:
bufferID = notepad.getCurrentBufferID()
if debug:
# Show console for debugging
console.clear()
console.show()
if expression != None:
expressionList = re.split(r"[\n\r]+", expression)
if debug:
console.write( expression + "\n" )
if len(expressionList) == 2:
if debug:
console.write( "'" + expressionList[0] + "', '" + expressionList[1] + "'\n" )
# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()
if debug:
console.write( 'editor.pyreplace( r"%s" % expressionList[0], r"%s" % expressionList[1], 0, re.IGNORECASE)\n' )
editor.pyreplace( r"%s" % expressionList[0], r"%s" % expressionList[1], 0, re.IGNORECASE)
# End the undo action, so Ctrl-Z will undo the above two actions
editor.endUndoAction()
# Debug
if debug:
notepad.activateBufferID(bufferID)
将此脚本链接到 Notepad++ 快捷方式(即 Ctrl+r)后,搜索
^([0-9]{7})(\t).*([0-9]{2})$
并替换为
\1\2\3
将此脚本映射到 Notepad++ 快捷方式Ctrl+<ChooseALetter>
并运行它。
我已经测试了这个脚本,效果很好!