在文本文件中查找并替换为正则表达式
Find and replace with a regular expression in a text file
我正在构建一个 jenkins 作业以进行构建比较。我想用单个换行符 (\n) 替换文本文件中的 双换行符 (\n\n)。之后我想用换行符替换 word "commit" 的每个实例并提交,即“\ncommit”。我想为此使用 vbscript,有人可以建议怎么做吗?
目前我正在使用以下 VBScript:
Const ForReading = 1
Const ForWriting = 2
strName = Wscript.Arguments(0)
strOriginal = Wscript.Arguments(1)
strReplacement = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strName, ForReading)
strText = objFile.ReadAll
objFile.Close
' Replace desired string
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.IgnoreCase = False
objRegExp.Pattern = strOriginal
strReplacement = objRegExp.Replace(strText, strReplacement)
Set objFile = objFSO.OpenTextFile(strName, ForWriting)
objFile.Write strReplacement
objFile.Close
脚本用普通字符串替换正则表达式模式,即用普通字符串替换双换行符,即字符串 \n
。我不知道如何扩展它以用正则表达式替换正则表达式。
正则表达式对此有点矫枉过正。使用两个字符串替换:
strText = objFSO.OpenTextFile(strName, ForReading).ReadAll
strText = Replace(strText, vbLf & vbLf, vbLf)
strText = Replace(strText, "commit", vbLf & "commit")
objFSO.OpenTextFile(strName, ForWriting).Write strText
我正在构建一个 jenkins 作业以进行构建比较。我想用单个换行符 (\n) 替换文本文件中的 双换行符 (\n\n)。之后我想用换行符替换 word "commit" 的每个实例并提交,即“\ncommit”。我想为此使用 vbscript,有人可以建议怎么做吗?
目前我正在使用以下 VBScript:
Const ForReading = 1
Const ForWriting = 2
strName = Wscript.Arguments(0)
strOriginal = Wscript.Arguments(1)
strReplacement = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strName, ForReading)
strText = objFile.ReadAll
objFile.Close
' Replace desired string
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.IgnoreCase = False
objRegExp.Pattern = strOriginal
strReplacement = objRegExp.Replace(strText, strReplacement)
Set objFile = objFSO.OpenTextFile(strName, ForWriting)
objFile.Write strReplacement
objFile.Close
脚本用普通字符串替换正则表达式模式,即用普通字符串替换双换行符,即字符串 \n
。我不知道如何扩展它以用正则表达式替换正则表达式。
正则表达式对此有点矫枉过正。使用两个字符串替换:
strText = objFSO.OpenTextFile(strName, ForReading).ReadAll
strText = Replace(strText, vbLf & vbLf, vbLf)
strText = Replace(strText, "commit", vbLf & "commit")
objFSO.OpenTextFile(strName, ForWriting).Write strText