I'm trying to match a regex and replace the match in a file. My regex is as follows (which matches fine):
$regex1 = [regex] "index.php\?title\=[a-zA-Z0-9_]*"
a redacted excerpt of the source file I'm trying to run the replace in:
<content:encoded>
<![CDATA[<a href="http://[redacted]/index.php?title=User_Manual">
<a href="http://[redacted]/index.php?title=User_Manual">The software</a>, running on the
<a href="http://[redacted]/index.php?title=Mobile_Device">POS Device</a>, enables
<a href="http://[redacted]/index.php?title=Logging_In">log in</a>,
<a href="http://[redacted]/index.php?title=Selecting_Journey">select a journey</a>
and the Powershell replacement:
.Replace("index.php?title=","").Replace("_","-").ToLower())
I've extracted all the matches, cast the $allmatches 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:
$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
}
At this point I have an array $newArray with all the regex matches and replacements, but no idea how to write this back to my file/variable e.g $newarray[0]:
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 4931
Length : 40
Value : user-manual
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).