Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string included dollar signs to -Replace Variable

Tags:

powershell

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?

like image 797
Ozie Harb Avatar asked Feb 05 '10 10:02

Ozie Harb


People also ask

Can you use the dollar sign in a variable name?

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 " _ ".

What does '$' mean in JavaScript?

$ 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).

Why is the dollar sign used for variables?

$ is used to DISTINGUISH between common variables and jquery variables in case of normal variables.

Can a dollar sign be in a variable Python?

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.


1 Answers

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'
like image 192
stej Avatar answered Sep 22 '22 12:09

stej