Python 正規表現の検索と置換には、 PythonScript Notepad++ プラグインを使用します。機能
についてはこちら
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>
実行します。
このスクリプトをテストしたところ、うまく機能しました。