查找/替换但增加值

6 html notepad++

我有一个.HTML文件,附有许多照片用于显示照片库.我正在用新的替换旧照片,我认为必须有一种比简单地复制和粘贴重复文件名更聪明的方式.

我希望能够在每次替换时替换和增加文件名,从我选择的基本文件名开始.例如...

...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg
Run Code Online (Sandbox Code Playgroud)

基本上执行CTRL + F和REPLACE'69thStreet010.jpg'与......

...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg
Run Code Online (Sandbox Code Playgroud)

等等,直到我希望它停止.这里有人有什么建议吗?非常感谢!

更新:我还应该补充一点,我正在使用Notepad ++来编辑我的文件.

Dil*_*e-O 10

查看Notepad ++中的列编辑器功能.您可以在要增加的区域上执行块选择,然后为其指定一个开始/结束范围,并且您已完成设置.

我最近处于类似情况.我做了一些正则表达式搜索/替换,让我的文件名变成我的"默认"格式,加上一些填充的"000",我希望我的增量开始.之后,我使用Shift+ ALT来阻止选择000并使用编辑器完成剩下的工作.


小智 7

好的,我想出了一个 Notepad++ 解决方案。
它确实需要您安装 Notepad++ 插件 PythonScript。它是一个类似 TextPad 的增量替换。


搜索和替换字符串中的整数值,
从 1 开始逐步替换(0 是默认的起始值):

在您的情况下,它将是
Search: ...images/69thStreet[0-9]+.jpg
替换: ...images/xyz00\i(1).jpg

安装 PythonScript 插件。
将下面的代码另存为 PythonScript 并为其关联一个快捷方式(即 Ctrl+i):
然后在 Notepad++ 中打开您的 html 文件并在您的 html 文件上运行以下脚本。
当搜索替换对话框提示时,输入以下两行:
...images/69thStreet[0-9]+.jpg
...images/xyz00\i(1).jpg

现在,
...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg 的 所有实例

将变成
...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg

这是 PythonScript 代码

from Npp import *
import re, string

# First we'll start an undo action, then Ctrl-Z will undo the actions of the whole script
editor.beginUndoAction()

expression     = notepad.prompt("Enter the search string on the first line, followed by Ctrl+Enter, \n" +
                                "followed by the replace string on second line",
                                "Incremental Search/Replace" ,
                                "")

expressionList = re.split(r"[\n\r]+", expression)

if len(expressionList) == 2:
    foundCount = [0]

    def incrementalReplace(parmReplaceStr,parmFoundCount):
        varPatternI  = r'\\i\((?P<starting>[0-9]+)\)'
        varPatternIi = r'\\i'
        varPatternISearch = re.search(varPatternI , parmReplaceStr)

        if varPatternISearch != None:
            varFoundCount    = int(varPatternISearch.group('starting')) + parmFoundCount[0]
            varReplaceString = re.sub(varPatternI ,str(varFoundCount    ),parmReplaceStr)
        elif re.search(varPatternIi, parmReplaceStr) != None:
            varReplaceString = re.sub(varPatternIi,str(parmFoundCount[0]),parmReplaceStr)

        parmFoundCount[0] = parmFoundCount[0] + 1    
        return varReplaceString

    # Do a Python regular expression replace
    editor.searchAnchor()
    while editor.searchNext(0x00200000,expressionList[0]) != -1:
        editor.replaceSel(incrementalReplace(expressionList[1],foundCount))
        editor.lineDown() 
        editor.searchAnchor()

    # End the undo action, so Ctrl-Z will undo the above two actions
    editor.endUndoAction()
Run Code Online (Sandbox Code Playgroud)


Dyk*_*kam 5

如果您的S&R支持Regex,请使用此功能.搜索词:

images/69thStreet([0-9]+).jpg
Run Code Online (Sandbox Code Playgroud)

替换术语:

images/xyz$1.jpg
Run Code Online (Sandbox Code Playgroud)


Fra*_*ger 1

现在是您学习脚本语言的时候了。Python/Ruby/Perl 可以通过使用简单的正则表达式和目录列表在几行内完成此操作。