【发布时间】:2017-12-18 08:51:20
【问题描述】:
我有一个如下所示的字幕 (.srt) 文件:
2
00:04:22,504 --> 00:04:23,520
Hello?
3
00:04:27,860 --> 00:04:29,112
Hey wait!
Hello!
3
00:06:18,860 --> 00:06:21,112
Uhh!
3
00:06:29,860 --> 00:06:32,112
Ah!
4
00:07:19,232 --> 00:07:21,284
What are you doing here?
5
00:07:21,608 --> 00:07:22,708
Tell me!
...
如您所见,数字 3 在该文件中重复了三次,我想通过重新编号整个字幕文件来替换它(因为我猜这是唯一的选择,因为这种重复在多个地方这个文件)。
我在下面创建了用于选择该文件的脚本,并试图用新生成的数字(迭代数字)替换重复的数字,但它不起作用。
Dim strFile, objFS
strFile = SelectFile( )
If strFile = "" Then
WScript.Echo "No file selected."
End If
Function SelectFile( )
Dim objExec, strMSHTA, wshShell
SelectFile = ""
strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _
& "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _
& ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>"""
Set wshShell = CreateObject( "WScript.Shell" )
Set objExec = wshShell.Exec( strMSHTA )
SelectFile = objExec.StdOut.ReadLine( )
Set objExec = Nothing
Set wshShell = Nothing
End Function
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.OpenTextFile(strFile)
Set objFile2 = objFS.OpenTextFile(strFile, 8, True)
x = 0
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = "^\d+$"
Set colMatches = objRegEx.Execute(strLine)
If colMatches.Count > 0 Then
x = x + 1
strLine = x
strNewLine = Replace(strLine,strLine,x)
objFile2.WriteLine strLine
End If
Loop
任何人都可以帮忙,弄清楚如何使它工作?
【问题讨论】:
-
可以用vbscript以外的其他技术来完成吗?
-
因为到目前为止我已经创建了 VBScript,所以我更喜欢基于 vbscript 的答案,但是如果您有有效的解决方案,请在评论中添加您的答案或将网址添加到您的脚本中。
标签: regex replace vbscript pattern-matching subtitle