Passing string included dollar signs to -Replace Variable

后端 未结 4 456
心在旅途
心在旅途 2021-01-18 07:58

I am trying to replace a sentence in .config file using powershell.

${c:Web.config} = ${c:Web.config} -replace

\'$BASE_PATH$\\

4条回答
  •  梦毁少年i
    2021-01-18 08:46

    This is about how to escape regexes. Every special character (special with regards to regular expressions) such as $ should be escaped with \

    '$A$B()[].?etc' -replace '\$|\(|\)|\[|\]|\.|\?','x'
    '$BASE_PATH$\Test\bin$Test_TYPE$\WebTest.dll' -replace '\$BASE_PATH\$\\Test\\bin\$Test_TYPE\$\\WebTest.dll','something'
    

    The backtick would be used when the regex would be like this:

    '$A$B' -replace "\`$",'x'
    

提交回复
热议问题