title: Find and Replace code between #region tags in .cs file
body: <p>I have a C# file called <code>test.cs</code>. It has a <code>#region</code> and <code>#endregion</code> declared for a section of code that I want to remove at a later date using VBScript through a batch file, and then save the contents out to a new file.</p>
  
  <p>Here is my current attempt:</p>
  
  <p><code>test.cs</code>:</p>
  
  <pre><code>#region 
  
  Console.WriteLine("test");
  
  #endregion
  </code></pre>
  
  <p>Then in my VBScript file:</p>
  
  <pre class="lang-vb prettyprint-override"><code>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) &amp; vbCrlf &amp; sReplStr _
                    &amp; vbCrlf &amp; oMatch.SubMatches(2))
          Next
      End If
      fReplaceText = temp
  End Function
  </code></pre>