Html 查找/替换但增加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1105621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:12:19  来源:igfitidea点击:

Find/Replace but Increment Value

htmlnotepad++

提问by Dillie-O

I have an .HTML file that has many photos attached to it for displaying a photo gallery. I'm replacing old photos with new ones, and I'm thinking there must be a more intelligent way than simply copying and pasting repetitive file names.

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

I would love to be able to replace and increment a file name on each replace, starting at a base file name of my choosing. For example...

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

...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg

Basically performing a CTRL+F and REPLACE '69thStreet010.jpg' with...

基本上执行 CTRL+F 并用...替换'69thStreet010.jpg'

...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg

And so on until I want it to stop. Anyone here have any suggestions? Thanks a lot!

依此类推,直到我想让它停止。这里有人有什么建议吗?非常感谢!

UPDATE: I should also add, I'm using Notepad++ to edit my files.

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

回答by Dillie-O

Look into the Column Editorfeature within Notepad++. You can do a block select on the area you want to increment and then give it a starting/ending range and you're all set.

查看Notepad++ 中的列编辑器功能。您可以在要增加的区域上进行块选择,然后给它一个开始/结束范围,然后就大功告成了。

I was in a similar situation recently. I did a little bit of regular expression search/replace to get my filenames into my "default" format with some padded "000" where I wanted my incrementing to start. After that, I used Shift+ALTto block select the 000s and used the editor to do the rest of the work.

我最近也处于类似的情况。我做了一些正则表达式搜索/替换,以将我的文件名转换为我的“默认”格式,并带有一些填充的“000”,我希望我的增量开始。之后,我使用Shift+ALT来阻止选择 000 并使用编辑器来完成剩下的工作。

回答by Dykam

If your S&R supports Regex, use this. Search term:

如果您的 S&R 支持 Regex,请使用它。搜索词:

images/69thStreet([0-9]+).jpg

Replace term:

替换术语:

images/xyz.jpg

回答by Vasiliy Deych

I just use Excel & Notepad++

我只使用 Excel 和 Notepad++

Create a column of incrementing numbers in Excel. Then in the next (or previous) column, put in the line you wish to merge w/ the incrementing numbers. For instance:

在 Excel 中创建一列递增数字。然后在下一列(或上一列)中,放入您希望与递增数字合并的行。例如:

file00  |  1  |  .jpg
file00  |  2  |  .jpg

Etc.

等等。

Then copy/paste the thing into Notepad++ (or use Excel's CONCATENATE function if the job is simple) and do a replacement.

然后将内容复制/粘贴到 Notepad++(如果工作简单,则使用 E​​xcel 的 CONCATENATE 函数)并进行替换。

回答by Vasiliy Deych

OK, I came up with a Notepad++ solution.
It does require you to install the Notepad++ plugin PythonScript. It is a TextPad-like incremental replace.

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

i.e.
Search and replace integer values within a string
replacing incrementally starting with 1 (0 is the default starting value):


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

In your case it would be
Search: ...images/69thStreet[0-9]+.jpg
Replace: ...images/xyz00\i(1).jpg

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

Install the PythonScript plugin.
Save the code below as a PythonScript and associate a shortcut (i.e. Ctrl+i) to it:
Then open your html file in Notepad++ and run the script below on your html file.
When prompted by the search replace dialogue, enter the following 2 lines:
...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

Now, all instances of
...images/69thStreet010.jpg
...images/69thStreet011.jpg
...images/69thStreet012.jpg

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

will become
...images/xyz001.jpg
...images/xyz002.jpg
...images/xyz003.jpg

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

Here is the PythonScript code

这是 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()

回答by Frank Krueger

It's time for you to learn scripting languages. Python/Ruby/Perl can do this in a few lines by using simple regular expressions and a directory listing.

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

回答by willy wonka

notepad++ allows you to make multi line editing. But the main problem here is to get as quick as possible all the file names from the directory where they are.

notepad++ 允许您进行多行编辑。但这里的主要问题是尽可能快地从它们所在的目录中获取所有文件名。

In the past I had similar challenge that I solved this way. This works almost on all win vers.

过去,我遇到过类似的挑战,我就是这样解决的。这几乎适用于所有获胜版本。

To make a list of all files in a directory create a bat file into the dir itself.

要列出目录中的所有文件,请在目录本身中创建一个 bat 文件。

copy and paste there the following batch code

复制并粘贴以下批处理代码

dir /b > filelist.txt

Save the file with name

使用名称保存文件

makefilelist.bat

Not important the name you give it: the important thing is the file extension .bat

你给它的名字不重要:重要的是文件扩展名 .bat

Double click on the bat file just saved: the batch script executes a clean dir command inside the directory in witch it is executed redirecting the output to the file filelist.txt

双击刚刚保存的 bat 文件:批处理脚本在执行它的目录中执行一个 clean dir 命令,将输出重定向到文件 filelist.txt

In less than one sec you have the output saved into the filelist.txt witch will be similar to the following:

在不到一秒的时间内,您将输出保存到 filelist.txt 中,类似于以下内容:

filelist.txt
Image File Name 01.gif
Image File Name 02.gif
Image File Name 03.gif
Image File Name 04.gif
Image File Name 05.gif
Image File Name 06.gif
Image File Name 07.gif
Image File Name 08.gif
Image File Name 09.gif
Image File Name 10.gif
Image File Name 11.gif
Image File Name 12.gif
Image File Name 13.gif
Image File Name 14.gif
Image File Name 15.gif
Image File Name 16.gif
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg
Image File Name 33.PNG
Image File Name 34.PNG
Image File Name 35.PNG
Image File Name 36.PNG
Image File Name 37.PNG
Image File Name 38.PNG
Image File Name 39.PNG
Image File Name 40.PNG
Image File Name 41.PNG
Image File Name 42.PNG
Image File Name 43.PNG
Image File Name 44.PNG
Image File Name 45.PNG
Image File Name 46.PNG
Image File Name 47.PNG
Image File Name 48.PNG
makelist.bat
filelist.txt
Image File Name 01.gif
Image File Name 02.gif
Image File Name 03.gif
Image File Name 04.gif
Image File Name 05.gif
Image File Name 06.gif
Image File Name 07.gif
Image File Name 08.gif
Image File Name 09.gif
Image File Name 10.gif
Image File Name 11.gif
Image File Name 12.gif
Image File Name 13.gif
Image File Name 14.gif
Image File Name 15.gif
Image File Name 16.gif
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg
Image File Name 33.PNG
Image File Name 34.PNG
Image File Name 35.PNG
Image File Name 36.PNG
Image File Name 37.PNG
Image File Name 38.PNG
Image File Name 39.PNG
Image File Name 40.PNG
Image File Name 41.PNG
Image File Name 42.PNG
Image File Name 43.PNG
Image File Name 44.PNG
Image File Name 45.PNG
Image File Name 46.PNG
Image File Name 47.PNG
Image File Name 48.PNG
makelist.bat

Of course the names logged into the file list may vary... The script logs every file, no matters if it is in sequence or not or if its name is a random name or contains date time (time stamp) etc.: it logs everything is into the same dir in witch is executed...

当然,记录到文件列表中的名称可能会有所不同......脚本会记录每个文件,无论它是否按顺序排列,或者它的名称是否是随机名称或包含日期时间(时间戳)等:它记录一切都在女巫的同一个目录中被执行......

Now you can open filelist.txt with notepad++: first delete the lines of the list that report the unwanted files:

现在你可以用notepad++打开filelist.txt:首先删除列表中报告不需要文件的行:

makelist.bat

制作清单.bat

filelist.txt

文件列表.txt

This operation is necessary cause the script lists also itself and the filelist.txt.

此操作是必要的,因为脚本还列出了自身和 filelist.txt。

Then with multi line editing tool add the tag around all lines at once, or everything else needed to built the photo gallery, even a javascript array, etc... in the end copy and paste the finished job to your image gallery file under coding. Done. Easy as drinking a glass of water: it is more difficult to explain than to do! Granted. ;-)

然后使用多行编辑工具一次在所有行周围添加标签,或者构建照片库所需的所有其他内容,甚至是 javascript 数组等......最后将完成的工作复制并粘贴到您的图片库文件中进行编码. 完毕。就像喝一杯水一样简单:解释比做更难!的确。;-)



Improvements:

改进:

It is possible to get a list of just the jpg files by modifying the batch command like the following:

可以通过修改批处理命令来获取 jpg 文件的列表,如下所示:

dir *.jpg /b > filelist.txt
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg
Image File Name 17.jpg
Image File Name 18.jpg
Image File Name 19.jpg
Image File Name 20.jpg
Image File Name 21.jpg
Image File Name 22.jpg
Image File Name 23.jpg
Image File Name 24.jpg
Image File Name 25.jpg
Image File Name 26.jpg
Image File Name 27.jpg
Image File Name 28.jpg
Image File Name 29.jpg
Image File Name 30.jpg
Image File Name 31.jpg
Image File Name 32.jpg

The same with other file extensions.

与其他文件扩展名相同。

回答by ryansstack

There is. Create your list of photos using a python script that looks at whatever directory you want, and renames or just lists all the file names. Then go from that to a python script that generates the HTML you need (should be very easy at this point). Then copy & paste that.

有。使用 python 脚本创建您的照片列表,该脚本查看您想要的任何目录,并重命名或仅列出所有文件名。然后从它转到生成您需要的 HTML 的 python 脚本(此时应该很容易)。然后复制并粘贴它。

Python is very easy to use, you will be able to download it in 5 minutes & find a tutorial for reading / changing file names in another 10 minutes.

Python 非常易于使用,您将能够在 5 分钟内下载它并在另外 10 分钟内找到阅读/更改文件名的教程。