Break a replace string in powershell - powershell

I am using powershell to automaticly add lines of code to certain scripts. See example below:
$a -replace '<div class="ef-column1 bodyContent" id="column1">', '<div class="ef-column1 body-content" id="column1"> #RenderSection("ColumnMainHeader", false)'
The #RenderSection part should be on a new line. So i tried to add 'n in front of #RenderSection but this will create 'n#RenderSection instead of putting #RenderSection on a new line.
I also tried 'r'n#RenderSection, but this has the same effect. putting 'n between " " will work neither.

The issue is with the single quote at the front making the grave a literal one rather than escaping the 'n.
Try using double quotes and then escaping all of the double quotes in the expression:
$a -replace '<div class="ef-column1 bodyContent" id="column1">', "<div class=`"ef-column1 body-content`" id=`"column1`"> `n#RenderSection(`"ColumnMainHeader`", false)"

Related

How to auto-escape a special char in VS Code Snippets?

I want to write a snippet for Debugging in TYPO3.
This is my Snippet-Code in php.json file:
"TYPO3 Extbase DebuggerUtility": {
"prefix": "ee",
"body": [
"\\TYPO3\\CMS\\Extbase\\Utility\\DebuggerUtility::var_dump($1,'$1');",
"$0"
],
"description": "TYPO3 Extbase DebuggerUtility"
},
If I want to debug something liket this : $this->settings['key'] I get this code:
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings['key']');
But it should looks like this
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings[\'key\']');
With escaped ' in the second part of that snippet.
EDIT
Thank you, but I think you missunderstood the question.
I don't want to escape a static character. I want to use the snippet and when I type the first $1-content it should be $this->settings['someKey'] but the second $1 (which is near the same) should automatically escape the ' chars I write, that I don't do this manually by hand.
So if i type '
first $1: ' second $1: \' that my Debug looks like this
Debug:
$this->settings['someKey']
contentOfsomeKey
I I don't escape the ' signs inside the "title of the debug" it breaks the string because ' wraps the debug-title.
In other words: I want to escape the content of the second $1 variable not the variable or the '-wrap in the snippet.
I hope I could clarify my issue.
If you want escape characters \ in your output you need to insert escaped escape characters: \\ this should result in single escape characters.
You might need an additional escape character if the following character needs an additional escaping: one backslash before quote \' = \\+ \' = \\\'
`

remove or ignore double quotes in a string - powershell

I think I have a simple problem but I can't seem to find the answer.
This is a part of the string I am working with
$text = "INDEPENDENT ELECTORAL AND BOUNDARIES COMMISSION
POLLING STATION: "ABC DEF GHIJKL (001)"
STREAM:123"
When I try to work with this, I get an error because of the double quotes in $text. I know I can escape the double quotes using a back tick, but the entire string is too big for me to go through it all.
I wonder if there is a simple way to ignore or remove all double (and single, too) quotes except for the first and last.
You can solve this by using here-strings:
$Text = #'
All the symbols I/ can ` hope for
between " the *>$ opening $() and closing ${}
of the symbols
'#
Use this: #' ... '#
Your string would look like this:
$text = #'
"INDEPENDENT ELECTORAL AND BOUNDARIES COMMISSION
POLLING STATION: "ABC DEF GHIJKL (001)"
STREAM:123"
'#

Issue with eval_in_page - Trying to interpolate an array

my #para_text = $mech->xpath('/html/body/form/table/tbody/tr[2]/td[3]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/div/div/div', type => $mech->xpathResult('STRING_TYPE'));
#BELOW IS JUST TO MAKE SURE THE ABOVE CAPTURED THE CORRECT TEXT
print "Look here: #para_text";
$mech->click_button( id => "lnkHdrreplyall");
$mech->eval_in_page('document.getElementsByName("txtbdy")[0].value = "#para_text"');
In the last line of my code I need to put the contents of the #para_text array as the text to output into a text box on a website however from the "document" till the end of the line it needs to be surrounded by ' ' to work. Obviously this doesnt allow interpolation as that would require " " Any ideas on what to do?
To define a string that itself contains double quotes as well as interpolating variable values, you may use the alternative form of the double quote qq/ ... /, where you can choose the delimiter yourself and prevent the double quote " from being special
So you can write
$mech->eval_in_page(qq/document.getElementsByName("txtbdy")[0].value = "#para_text"/)

Getting Double Quotation Marks in Write-Host Output

I need to get some double quotations around the GUID=$ntds output. I have tried encompassing the entire string in double quotes to no avail. Single quotes won't work because of the variable.
$Site=Read-Host "ENTER SITE NAME"
$Server=Read-Host "ENTER SERVER NAME"
$NTDS=Get-ADObject -Identity "CN=NTDS Settings,CN=$server,CN=Servers,CN=$site,CN=Sites,CN=Configuration,$((Get-ADDomain).DistinguishedName)" |foreach {$_.objectguid}
write-host "Repadmin /showobjmeta" * "<GUID=$ntds>"
You can use another pair of double quotes to escape like
Write-Host "hello ""200mg"""
Which will output hello "200mg"
Your case it would be
write-host "Repadmin /showobjmeta""<GUID=$ntds>"""
I do not know powershell, but from other languages, you can try:
you can try using double-double quote "" where you want to have ".
"<GUID=""$ntds"">"
or you can try single quotes if permitted on the outside
'<GUID="$ntds">'
or you can try the backslash as escape character before the " making it
"<GUID=\"$ntds\">"
Just try and let me know if you succeeded.

Matlab: How to print " ' " character

I am trying to create the following string:
javaaddpath ('C:\MatlabUserLib\ParforProgMonv2')
However, I could only do the following
command = sprintf('%s ', varargin{1}, '(', varargin{2}, ')');
and that gives me:
javaaddpath ( C:\MatlabUserLib\ParforProgMonv2 )
UPDATE:
Based on Dan's suggestion, I used the following:
command = sprintf('%s', varargin{1}, '(', '''', varargin{2}, '''', ')')
Use two single quotation marks. See the docs for formatting strings, btw this concept is known as an escape character (to help you google such things in the future).
command = sprintf('%s ', varargin{1}, '(''', varargin{2}, ''')')
Although I think you might prefer
command = sprintf('%s (''%s'')', varargin{1}, varargin{2})
or if you have no other varargins (which I guess is very unlikely but anyway)
command = sprintf('%s (''%s'')', varargin{:})
There are a couple of ways around this. First you could declare your path as a string variable then pass the string to your command, eg,
path = 'my/path'
javaaddpath (path)
Or you can use special characters to insert things like a single quote or a new line character, so for a single quote,
EDIT: wrong display command as pointed out by Dan below
myString = '" Hi there! "'
disp(myString)