Reputation: 75
I have a question which im pretty much stuck on..
I have a file called xml_data.txt and another file called entry.txt
I want to replace everything between <core:topics> and </core:topics>
I have written the below script
$test = Get-Content -Path ./xml_data.txt
$newtest = Get-Content -Path ./entry.txt
$pattern = "<core:topics>(.*?)</core:topics>"
$result0 = [regex]::match($test, $pattern).Groups[1].Value
$result1 = [regex]::match($newtest, $pattern).Groups[1].Value
$test -replace $result0, $result1
When I run the script it outputs onto the console it doesnt look like it made any change.
Can someone please help me out
Note: Typo error fixed
Upvotes: 5
Views: 5548
Reputation: 626802
There are three main issues here:
.
does not match a newline by default$
symbol. Or use simple string .Replace
.So, you need to
$test = Get-Content -Path ./xml_data.txt -Raw
$pattern = "(?s)<core:topics>(.*?)</core:topics>"
regex (it can be enhanced in case it works too slow by unrolling it to <core:topics>([^<]*(?:<(?!</?core:topics>).*)*)</core:topics>
)$test -replace [regex]::Escape($result0), $result1.Replace('$', '$$')
to "protect" $
chars in the replacement, or $test.Replace($result0, $result1)
.Upvotes: 4