用 powershell 替换 link 中的字符

Replace character from link with powershell

我想用

替换 ?

"EquipmentInfo["?"] = "<iframe src='http://bing.fr'></iframe>";"

通过一个变量。

我试过这个:

(get-content C:\word.txt) -replace '?', '$obj' | Set-Content C:\word.txt

这个回答解释原来的问题。
提供全面的解决方案.

-replace运算符将正则表达式作为RHS上的第一个操作数,其中?是所谓的具有特殊含义的元字符.

因此,要使用文字 ?,您必须转义它,使用\

(get-content C:\word.txt) -replace '\?', $obj   

注意:不要在$obj周围使用'...',除非你想要文字字符串$obj;通常,要在字符串中引用变量,您必须使用 "...",但这里没有必要。

一个带有文字的简单示例:

'Right?' -replace '\?', '!' # -> 'Right!'

我会使用积极的回顾来确保您找到正确的问号。您还必须在替换中使用 双引号 ,因为您要替换变量:

(get-content C:\word.txt -raw) -replace '(?<=EquipmentInfo\[")\?', "$obj" | Set-Content C:\word.txt

使用正则表达式:

(?<=EquipmentInfo\[")\?