Quoting -replace & variables

38,096

Solution 1

When you use single quotes you tell PowerShell to use a string literal meaning everything between the opening and closing quote is to be interpreted literally.

When you use double quotes, PowerShell will interpret specific characters inside the double quotes.

See get-help about_quoting_rules or click here.

The dollar sign has a special meaning in regular expressions and in PowerShell. You want to use the single quotes if you intend the dollar sign to be used as the regular expression.

In your example the regex a(\w) is matching the letter 'a' and then a word character captured in back reference #1. So when you replace with $1 you are replacing the matched text ab with back reference match b. So you get bc.

In your second example with using double quotes PowerShell interprets "$1" as a string with the variable $1 inside. You don't have a variable named $1 so it's null. So the regex replaced ab with null which is why you only get c.

Solution 2

In your second line:

'abc' -replace 'a(\w)', "$1"

Powershell replaces the $1 before it gets to the regex replace operation, as others have stated. You can avoid that replacement by using a backtick, as in:

'abc' -replace 'a(\w)', "`$1"

Thus, if you had a string in a variable $prefix which you wanted to include in the replacement string, you could use it in the double quotes like this:

'abc' -replace 'a(\w)', "$prefix`$1"

Solution 3

The '$1' is a regex backreference. It's created by the regex match, and it only exists within the context of that replace operation. It is not a powershell variable.

"$1" will be interpreted as a Powershell variable. If no variable called $1 exists, the replacement value will be null.

Solution 4

Since I cannot comment or upvote, David Rogers' answer worked for me. I needed to use both RegEx backreference as well as a Powershell variable in a RexEx replace.

I needed to understand what the backtick did before I implemented it, here is the explanation: backtick is Powershell's escape character.

My usecase

$new = "AAA"
"REPORT.TEST998.TXT" -Replace '^([^.]+)\.([^.]+)([^.]{3})\.', "`$1.`$2$new."

Result

REPORT.TESTAAA.TXT

Alternatives

Format string

"REPORT.TEST998.TXT" -Replace '^([^.]+)\.([^.]+)([^.]{3})\.', ('$1.$2{0}.' -f )

Comments

as per https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html I'll probably use the format string method to avoid the use of backticks.

Share:
38,096
Vippy
Author by

Vippy

Updated on May 23, 2021

Comments