I want to unescape PHP code that has been escaped with backslashes. I'm using Powershell to do this.
Backslash characters can be used to escape a number of things, but in my sample data I only see examples of double-quotes being escaped in this manner. Ideally my code would support the other cases, but this is all I have for now.
$test = 'some-text.'
$test -replace '\"', '"'
The output of this is not what I expected:
some-text.
Expected output:
some-text.
I've also tried
$test -replace "`\`"", "`""
But the result was the same.
OK it seems backslashes are also used for escaping backslashes in powershell. Therefore the correct code is
$test -replace '\\"', '"'
Related
I have a string written in XML which approximates to:
60-894-74987
I need a line in powershell that will remove the '-' in the string to leave me with '6089474987'
I've been trying to utilize the -replace function, but it either errors or breaks powershell.
the basic code is:
$serialnumber= $pcinfo[4] #([4]= <value>60-984-74987</value>)
$serialnumber= $serialnumber replace '-',"
I dont get an error with this, powershell gives me >>
Change
$serialnumber= $serialnumber -replace '-',"
to
$serialnumber= $serialnumber -replace '-'
If you're just deleting characters and not replacing them, you don't need explicit empty quotes.
You can simply write as C# fashion below.
$serialnumber = $serialnumber.replace("-","")
I'm writing a powershell program to replace strings using
-replace "$in", "$out"
It doesn't work for strings containing a backslash, how can I do to escape it?
The -replace operator uses regular expressions, which treat backslash as a special character. You can use double backslash to get a literal single backslash.
In your case, since you're using variables, I assume that you won't know the contents at design time. In this case, you should run it through [RegEx]::Escape():
-replace [RegEx]::Escape($in), "$out"
That method escapes any characters that are special to regex with whatever is needed to make them a literal match (other special characters include .,$,^,(),[], and more.
You'll need to either escape the backslash in the pattern with another backslash or use the .Replace() method instead of the -replace operator (but be advised they may perform differently):
PS C:\> 'asdf' -replace 'as', 'b'
bdf
PS C:\> 'a\sdf' -replace 'a\s', 'b'
a\sdf
PS C:\> 'a\sdf' -replace 'a\\s', 'b'
bdf
PS C:\> 'a\sdf' -replace ('a\s' -replace '\\','\\'), 'b'
bdf
Note that only the search pattern string needs to be escaped. The code -replace '\\','\\' says, "replace the escaped pattern string '\\', which is a single backslash, with the unescaped literal string '\\' which is two backslashes."
So, you should be able to use:
-replace ("$in" -replace '\\','\\'), "$out"
[Note: briantist's solution is better.]
However, if your pattern has consecutive backslashes, you'll need to test it.
Or, you can use the .Replace() string method, but as I said above, it may not perfectly match the behavior of the -replace operator:
PS C:\> 'a\sdf'.replace('a\\s', 'b')
a\sdf
PS C:\> 'a\sdf'.replace( 'a\s', 'b')
bdf
Here is my sub-expression:
"""$((($l -split " """)[1] -split """ ")[0])"""
I have checked and found no unpaired parentheses. However powershell insists in saying "Missing closing ')' in expression."
Interestingly, the expression
$((($l -split " """)[1] -split """ ")[0])
works fine.
Has anyone had similiar experience before? Is it a bug of Powershell?
This.. is really interesting, and yes I would consider it a bug at least so far.
Here's a much simpler repro:
"$("`"")"
"$("""")"
It seems to be caused by using either form of double quote escaping (backtick or double double quote), inside a sub-expression, inside an expandable string.
It also appears that this is error comes right down to the parser itself:
$sb = #'
"$("`"")"
'#
$tokens = $null
$er = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($sb, [ref]$tokens, [ref]$er)
$ast.EndBlock.Statements[0].PipelineElements[0].Expression.NestedExpressions
$tokens[0].NestedTokens
The nested tokens/expressions it finds just aren't correct.
I tested with Windows PowerShell 5.1 and PowerShell Core 6.0.0-rc.2 on WLS.
Relevant Issues
Two double-quotes in inline expressions
String in Sub-Expression incorrectly parsed
Powershell: Doubled double quotes in inline expressions
this is part of my bigger code.
I have not added any assemblies for this. I want to get '20161207' separately in a different variable. This approach however is failing with: "parsing "*" - Quantifier {x,y} following nothing."
[string]$filter = '20161207*'
$pathPart = $filter -split '*'
echo $pathPart[0]
please help. I am using powershell version 4.0.
I am not sure what is the escape character in my version of powershell. i Have tried '/'
I want to echo out: '20161207'
The -split operator uses regular expressions, not literal strings. Since * is a quantifier in regexes what you got there is invalid. You need to escape it:
$filter -split '\*'
or use the string.Split method instead:
$filter.Split('*')
which splits on single characters.
I am trying to use PowerShell do a simple find and replace. I use template text files and use $ in front of values that need to be changed.
Example:
(Get-Content "D:\test") | Foreach-Object {$_ -replace "`$TBQUAL", "DBO"} | Set-Content "D:\test"
It should find the line OWNER=$TBQUAL and make it look like OWNER=DBO.
I am using the escape in front of $TBQUAL with no luck. To test that it is working if I removed the $ from the front, it would replace TBQUAL and made it look like OWNER=$DBO.
Two things to get this to work:
Use single quotes for your strings so that the $ is not interpreted as the start of a variable to be expanded.
Escape the $ using a backslash "\" so the regular expression parser takes it literally.
For example,
PS C:\> 'Has a $sign in it' -replace 'a \$sign', 'no dollar sign'
Has no dollar sign in it
If you aren't using regular expressions in your replacement, you can do a simple (and fast) replace like this:
Foreach-Object {$_.Replace('$TBQUAL', 'DBO')}