使用PythonScript Notepad++ 插件实现 Python 正则表达式搜索功能。
功能见here
Editor.pysearch(expression, function[, flags[, startLine[, endLine]]])
或
Editor.pymlsearch(expression, function[, flags[, startPosition[, endPosition]]])
这是一个使用python正则表达式搜索函数editor.pysearch()的简单程序
我留下了很多调试代码,所以你可以看到函数运行过程中发生了什么。
# $Revision: 1.3 $
# $Author: dot $
# $Date: 2012/04/19 00:03:26 $
from Npp import *
import re, string
expression = notepad.prompt(
"Enter the RegEx search string then press Enter",
"RegEx and Search" ,
"")
debug = True
#debug = False
if debug:
bufferID = notepad.getCurrentBufferID()
if debug:
# Show console for debugging
console.clear()
console.show()
if expression != None:
expressionSansNl = expression.replace('\r\n', '')
if debug:
console.write( expression + "\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.pysearch( r"%s" % expressionSansNl, None)\n' )
editor.pysearch( r"%s" % expressionSansNl, None)
# End the undo action, so Ctrl-Z will undo the above two actions
editor.endUndoAction()
# Debug
if debug:
notepad.activateBufferID(bufferID)
将此脚本映射到 Notepad++ 快捷方式 Ctrl+<ChooseALetter> 并运行它。
输入搜索 RegEx action(421|732|983)
根据 Python re 文档here,支持| RegEx 运算符。
我还没有测试过——也许 pysearch() 函数中的 None 参数需要
成为一个将选择结果字符串的函数。根据文档here,
该函数应该返回一个 python MatchObject。我认为有办法获得
MatchObject 中的匹配字符串并选择该匹配项的下一个外观
编辑器对象中的字符串。完成编码后,我将发布答案的新修订版
并对此进行测试。