Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

-replace will only work with "string", but not with $variable

$
0
0

Hi,

 

I'm trying to read some replacement rules from a file (separated by "|") and use them on a given Text...

 

$inputText = [system.IO.File]::ReadAllText("input.txt")

 

$regex = Get-Content "changes.txt" -ReadCount 0

foreach($expression in $regex) {

 if ($expression -eq 'EOF') { break }

    $parts = $expression.Split("|")

 if ($parts.Count -eq 2) {

$t = $parts[0]

$u = $parts[1]

   $inputText = $InputText -creplace $t, $u

   $inputText | out-file "output.txt" -enc ascii

  }

 }

 

 

This works fine for most rules, but doesn't work for matching patterns at end of a line.

e.g. \\DE`r`n|\\DE_02`r`n

 

If I try using strings for the replacement, the expected replacements are done:

  $inputText = $InputText -creplace "\\DE`r`n", "\\DE_02`r`n"

 

How come it works with "strings", but not with &variables??? (the variables $t and $u contain just the same strings...)

 

Which replacement rule can be used for my needs?

 

Thanks in advance!


Viewing all articles
Browse latest Browse all 10624

Trending Articles