我需要在VBScript或批处理中使用代码替换文本文件行中的5个字符(下面的粗体数字)来更改端口号。
change_port.vbs
:
prefsFile = "%userprofile%\Desktop\teste.msrcincident"
prefsFile = CreateObject("WScript.Shell").ExpandEnvironmentStrings(prefsFile)
newPrefs = "5500"
Set fso = CreateObject("Scripting.FileSystemObject")
json = fso.OpenTextFile(prefsFile).ReadAll
Set re = New RegExp
re.Pattern = "":*?",*,"
json = re.Replace(json, ": & newPrefs & ",*,")
fso.OpenTextFile(prefsFile, 2).Write(json)
原始文本文件:
RCTICKET="65538,1,10.0.0.1:54593,*,ucIdnri2n4QPf/bv92mtx4w2qliCNdyDgBpHPr7nJFdxYL2/dR+iel9Mh4zgD6QR,*,*,Fbjf5rcIrdrlnibnisrzRcO8tsY=" PassStub="HG)7HbhIZPTiKy" RCTICKETENCRYPTED="1" DtStart="1457700115" DtLength="142560" L="0"/></UPLOADINFO>
预期结果文本文件:
RCTICKET="65538,1,10.0.0.1:5500,*,ucIdnri2n4QPf/bv92mtx4w2qliCNdyDgBpHPr7nJFdxYL2/dR+iel9Mh4zgD6QR,*,*,Fbjf5rcIrdrlnibnisrzRcO8tsY=" PassStub="HG)7HbhIZPTiKy" RCTICKETENCRYPTED="1" DtStart="1457700115" DtLength="142560" L="0"/></UPLOADINFO>
任何人都可以帮助我吗?
答案 0 :(得分:0)
您的搜索和替换表达式搞砸了。您正在寻找冒号(:
),后跟一个或多个数字(\d+
或[0-9]+
),后跟逗号(,
),并希望用冒号后跟新的端口号和逗号替换它。
改变这个:
re.Pattern = "":*?",*,"
json = re.Replace(json, ": & newPrefs & ",*,")
进入这个。
re.Pattern = ":\d+,"
json = re.Replace(json, ":" & newPrefs & ",")
始终保持表达尽可能简单。