我需要将文本文件中的所有数字(不是单词的一部分)加 1。我尝试使用该模式\b(\d+)\b
来捕获所有这些数字,但我无法在文件中增加(向它们添加 1)它们。
输入
text1
1 5 7
hello world 5. This is Samurai 10 not samurai10.
text2
预期产出
text1
2 6 8
hello world 6. This is Samurai 11 not samurai10.
text2
我的尝试
const forReading = 1
set objFso = createObject("scripting.filesystemobject")
strFilePath = split(wscript.scriptFullName,wscript.scriptName)(0) & "haha.txt"
set objFile = objFso.openTextFile(strFilePath, forReading)
set objReg = new RegExp
With objReg
.Global = True
.ignoreCase = true
.multiline = true
.pattern = "\b(\d+)\b"
End With
strTemp = objFile.readAll()
strTemp = objReg.replace(strTemp,cint("$1")+1) '<--Here, I am getting the "Type mismatch 'Cint'" error. I just wanted to increment the number which was captured in Group 1
msgbox strTemp
set objFile = Nothing
set objFso = Nothing
如果我更换线strTemp = objReg.replace(strTemp,cint("$1")+1)
用strTemp = objReg.replace(strTemp,"_$1_")
,我得到下面的输出,这意味着我能得到哪些需要递增所需的数字:
我只是无法增加它们。真的很感激任何帮助!!!
只需替换代码:
strTemp = objReg.replace(strTemp,cint("$1")+1)
和
set objMatches = objReg.execute(strTemp)
for each match in objMatches
strTemp = left(strTemp, match.firstIndex) & replace(strTemp, match.value, cInt(match.value)+1, match.firstIndex+1, 1)
next
msgbox strTemp
这是我得到的输出:
检查替换功能的参考
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句