title: Powershell regex to replace urls in file
body: <p>I'm trying to match a regex and replace the match in a file.  My regex is as follows (which matches fine):</p>
  
  <pre><code>$regex1 = [regex] "index.php\?title\=[a-zA-Z0-9_]*"
  </code></pre>
  
  <p>a redacted excerpt of the source file I'm trying to run the replace in:</p>
  
  <pre><code>&lt;content:encoded&gt;
      &lt;![CDATA[&lt;a href="http://[redacted]/index.php?title=User_Manual"&gt;
      &lt;a href="http://[redacted]/index.php?title=User_Manual"&gt;The software&lt;/a&gt;, running on the 
      &lt;a href="http://[redacted]/index.php?title=Mobile_Device"&gt;POS Device&lt;/a&gt;, enables 
      &lt;a href="http://[redacted]/index.php?title=Logging_In"&gt;log in&lt;/a&gt;, 
      &lt;a href="http://[redacted]/index.php?title=Selecting_Journey"&gt;select a journey&lt;/a&gt;
  </code></pre>
  
  <p>and the Powershell replacement:</p>
  
  <pre><code>.Replace("index.php?title=","").Replace("_","-").ToLower())
  </code></pre>
  
  <p>I've extracted all the matches, cast the <code>$allmatches</code> array to a new array (so it would be writable), and then updated the values in the new array.  I cannot work out how to write this back to the file, and don't seem to be able to find any posts or documentation to help with this. My code to date:</p>
  
  <pre><code>$regex1 = [regex] "index.php\?title\=[a-zA-Z0-9_]*"
  
  $contentOf=Get-Content $contentfile
  $allmatches=$regex1.Matches($contentOf)
  $totalcount=$allmatches.Count
  
  $newArray = $allmatches | select *
  
  for($i=0;$i -le $totalCount;$i++) {
      $newvalue=(($allmatches[$i].Value).Replace("index.php?title=","").Replace("_","-").ToLower())
      $newArray[$i].Value = $newvalue
  }
  </code></pre>
  
  <p>At this point I have an array <code>$newArray</code> with all the regex matches and replacements, but no idea how to write this back to my file/variable e.g <code>$newarray[0]</code>:</p>
  
  <pre><code>Groups   : {0}
  Success  : True
  Name     : 0
  Captures : {0}
  Index    : 4931
  Length   : 40
  Value    : user-manual
  </code></pre>
  
  <p>Of course I may be going about this completely the wrong way.  As far as why I've chosen Powershell to do this, is simply because that is where I've spent most time scripting these days...of course I'm sure it would be achievable in shell (it would just take me longer to get there).</p>