Home > Enterprise >  -Replace Operator not accepting capture group variable and user variable at once
-Replace Operator not accepting capture group variable and user variable at once

Time:08-18

In the below example if I use single quotes for the -Replace operator ('Tag000: (.*)', 'Tag000: $1 hello $scategory') the capture group variable ($1) gets resolved at run time, while $scategory does not. If I use double quotes (-replace "Tag000: (.*)", "Tag000: $1 hello $scategory") the $scategory variable works but then the capture group varibale is not recognised

$scategory = "my car is red "
$Read_Opus_Category= Get-Content -Path "C:\Temp\Chapter 6.ps1" -Stream multitags.txt -Raw
$Modified_Opus_Category= $Read_Opus_Category -replace 'Tag000: (.*)', "Tag000: $1 hello $scategory"

Am at a loss here, any help would be wonderfull

CodePudding user response:

Use ` to escape the $ in an expandable string literal:

$Read_Opus_Category -replace 'Tag000: (.*)', "Tag000: `$1 hello $scategory"
  • Related