PowerShell: Quoting -replace & variables

浪子不回头ぞ 提交于 2019-12-30 03:55:08

问题


This is in response to my previous question:

PowerShell: -replace, regex and ($) dollar sign woes

My question is: why do these 2 lines of code have different output:

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

AND according to the 2 articles below, why doesn't the variable '$1' in single quotes get used as a literal string? Everything in single quotes should be treated as a literal text string, right?

http://www.computerperformance.co.uk/powershell/powershell_quotes.htm
http://blogs.msdn.com/b/powershell/archive/2006/07/15/variable-expansion-in-strings-and-herestrings.aspx


回答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.




回答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"




回答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.




回答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.



来源:https://stackoverflow.com/questions/9422669/powershell-quoting-replace-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!