I am trying to replace a sentence in .config file using powershell.
${c:Web.config} = ${c:Web.config} -replace
'$BASE_PATH
$\Test\bin`$Test_TYPE`$\WebTest.dll' , 'c:\program Files\example\webtest.dll'
Everytime I try to run the above code I get
"Invalid regular expression pattern: $BASE_PATH$\Test\bin\$Test_TYPE$\WebTest.dll" at c:\tests\runtesting.ps1 -replace <<<< $BASE_PATH$\Test\bin\$Test_TYPE$\WebTest.dll
If I don't use the backtick the dollar signs will disappear and some text.
How would I pass dollar signs in a string to -replace?
A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign " $ ", or the underscore character " _ ". The convention, however, is to always begin your variable names with a letter, not " $ " or " _ ".
$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).
$ is used to DISTINGUISH between common variables and jquery variables in case of normal variables.
dollar sign ('$') is an illegal character. class is a Python keyword with its special syntax. It can't be used a variable name. A name cannot contain a space Otherwise put, the space character (' ') is illegal in a name, just as '$' is.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With