在Powershell中,如何替换包含问号的字符串?

时间:2017-10-31 00:17:37

标签: powershell replace

在Powershell中,如何替换包含问号的字符串?例如:

(Get-Content file.txt) -replace "Hello?","Hello."

问号似乎被解释为某种特殊的性格。有办法逃脱吗?我尝试使用一两个反引号,但没有成功。

1 个答案:

答案 0 :(得分:6)

-replace运算符使用正则表达式模式匹配。在RegEx中,问号是一个量词,表示前一个匹配应匹配零次或一次。你可以通过在它前面放一个反斜杠来逃避问号:

(Get-Content file.txt) -replace "Hello\?","Hello."