I have a string in the form -content-, and I would like to replace it with &content&. How can I do this with replace in PowerShell?
PowerShell strings are just .NET strings, so you can:
PS> $x = '-foo-'
PS> $x.Replace('-', '&')
&foo&
...or:
PS> $x = '-foo-'
PS> $x.Replace('-foo-', '&bar&')
&bar&
Obviously, if you want to keep the result, assign it to another variable:
PS> $y = $x.Replace($search, $replace)
The built-in -replace operator allows you to use a regex for this e.g.:
C:\PS> '-content-' -replace '-([^-]+)-', '&$1&'
&content&
Note the use of single quotes is essential on the replacement string so PowerShell doesn't interpret the $1 capture group.
Related
I run a cmdlet in powershell which returns a string eg;
123##EXT#outlook.com
456#outlook.com
789#outlook.com
I'm looking for a way to remove the #EXT# characters from the string. I can do everything but remove this and am struggling to find any documentation :/
Lets assume the cmdlet outputs an array of strings. This is simulated by the $adr variable.
#Array of strings
$adr = #(
"123##EXT#outlook.com"
"456#outlook.com"
"789#outlook.com"
)
$adr|foreach {$_ -replace "#EXT#",""}
Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
Double quotes allow variable expansion while single quotes do not:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
For example:
$i = 5
"The value of $i is $i."
The output of this command is:
The value of 5 is 5.
Single-quoted strings
A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:
$i = 5
'The value of $i is $i.'
The output of this command is:
The value of $i is $i.
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).
I have the following string: $str = '"FirstName":"first name","LastName":"Last name","AskCatalog":false,"Nuteres":61","ZipCode":"1234"'
and I need to replace value of ,for example, FirstName:"first name" with a variable like this
"FirstName":"$strFristname"
Can anyone show me how can I do that in PowerShell?
Thank you.
Like this:
$str -replace '("FirstName":)".*?"', "`$1`"$strFirstname`""
The pattern ("FirstName":)".*?" matches the string "FirstName": followed by a double quote and the shortest match of any character (.*?) up to the next double quote. The parentheses create a group that can be referenced by $1 in the replacement string. Due to the double quotes around the replacement string that reference must be escaped (`$1). The same goes for the nested double quotes (`").
If you want the variable instead of its value to show up in the result, you need to escape the $ of the variable as well:
$str -replace '("FirstName":)".*?"', "`$1`"`$strFirstname`""
Demonstration:
PS C:\> $str = '"FirstName":"first name","LastName":"Last name"'
PS C:\> $strFirstname = 'Joe'
PS C:\> $str -replace '("FirstName":)".*?"', "`$1`"$strFirstname`""
"FirstName":"Joe","LastName":"Last name"
PS C:\> $str -replace '("FirstName":)".*?"', "`$1`"`$strFirstname`""
"FirstName":"$strFirstname","LastName":"Last name"
What is the opposite function of the following?
$tmp = $domain.split(".")
In other words, how do I transfer an array to a string. I've heard of "join", but I can't get it to work.
Non-cmdlet, non-quest, this has to work with PowerShell v1 and all of the older systems!
There's a join operator in Powershell:
$tmp -join "."
$result = [string]::join(".", $array)
In addition to the Join operator (introduced in v2) and the .NET Join method, you can also change the value of the special variable $OFS (Ouptut Field Seperator) to a dot (the default value is a space) and enclose tmp in quotes:
PS> $tmp = "foo.domain.com".split(".")
PS> & {$ofs='.';"$tmp"}
foo.domain.com
In the above example I changed $OFS in a script block to prevent it from changing in the global scope (scriptblocks creates nested scope)
Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
Double quotes allow variable expansion while single quotes do not:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
For example:
$i = 5
"The value of $i is $i."
The output of this command is:
The value of 5 is 5.
Single-quoted strings
A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:
$i = 5
'The value of $i is $i.'
The output of this command is:
The value of $i is $i.
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).