在 .cs 文件中的 #region 标签之间查找和替换代码
Find and Replace code between #region tags in .cs file
我有一个名为 test.cs
的 C# 文件。它有一个 #region
和 #endregion
声明用于我想在以后通过批处理文件使用 VBScript 删除的一段代码,然后将内容保存到一个新文件中。
这是我目前的尝试:
test.cs
:
#region
Console.WriteLine("test");
#endregion
然后在我的 VBScript 文件中:
strInputFile = "test.cs"
strOutputFile = "testOutput.cs"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strOutputFile) Then
fso.DeleteFile strOutputFile
End If
strPattern = "(/#region/)(.|\s)*(/#endregion/)"
strReplaceString = "x"
strTestString = fso.OpenTextFile(strInputFile, 1).ReadAll
strNewText = fReplaceText(strPattern, strTestString, strReplaceString)
fso.OpenTextFile(strOutputFile, 2, True).WriteLine strTestString
Function fReplaceText(sPattern, sStr, sReplStr)
Dim regEx, oMatch, colMatches, temp
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set colMatches = regEx.Execute(sStr) ' Execute search.
If colMatches.Count = 0 Then
temp = ""
Else
For Each oMatch In colMatches
temp = regEx.Replace(sStr, oMatch.SubMatches(0) & vbCrlf & sReplStr _
& vbCrlf & oMatch.SubMatches(2))
Next
End If
fReplaceText = temp
End Function
该模式应该在 #region
和 #endregion
之间寻找 'everything' 的(可能为空)序列 non-greedily:
Option Explicit
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "#region[\s\S]*?#endregion"
WScript.Echo r.Replace(Join(Array( _
"a" _
, "#region#endregion" _
, "#region" _
, "a" _
, "a" _
, "#endregion" _
, "a" _
), vbCrLf), "x")
输出:
cscript 30735491.vbs
a
x
x
a
我有一个名为 test.cs
的 C# 文件。它有一个 #region
和 #endregion
声明用于我想在以后通过批处理文件使用 VBScript 删除的一段代码,然后将内容保存到一个新文件中。
这是我目前的尝试:
test.cs
:
#region
Console.WriteLine("test");
#endregion
然后在我的 VBScript 文件中:
strInputFile = "test.cs"
strOutputFile = "testOutput.cs"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strOutputFile) Then
fso.DeleteFile strOutputFile
End If
strPattern = "(/#region/)(.|\s)*(/#endregion/)"
strReplaceString = "x"
strTestString = fso.OpenTextFile(strInputFile, 1).ReadAll
strNewText = fReplaceText(strPattern, strTestString, strReplaceString)
fso.OpenTextFile(strOutputFile, 2, True).WriteLine strTestString
Function fReplaceText(sPattern, sStr, sReplStr)
Dim regEx, oMatch, colMatches, temp
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = sPattern ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set colMatches = regEx.Execute(sStr) ' Execute search.
If colMatches.Count = 0 Then
temp = ""
Else
For Each oMatch In colMatches
temp = regEx.Replace(sStr, oMatch.SubMatches(0) & vbCrlf & sReplStr _
& vbCrlf & oMatch.SubMatches(2))
Next
End If
fReplaceText = temp
End Function
该模式应该在 #region
和 #endregion
之间寻找 'everything' 的(可能为空)序列 non-greedily:
Option Explicit
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "#region[\s\S]*?#endregion"
WScript.Echo r.Replace(Join(Array( _
"a" _
, "#region#endregion" _
, "#region" _
, "a" _
, "a" _
, "#endregion" _
, "a" _
), vbCrLf), "x")
输出:
cscript 30735491.vbs
a
x
x
a