This question already has an answer here:
powershell: how to escape all regex characters from a string
(1 answer)
Closed 4 years ago.
I need to replace ^~^ with a tab (\t) in a CSV file in PowerShell.
I tried escaping caret with a backtick (`^~`^) but it doesn't work. How do I do this?
$file=Import-Csv $dataFilePath
$file -replace "\`^~\`^", "\`t"
Is that what you're looking for?
>$s = "^~^"
>$s -replace "\^\~\^", "``t"
`t
Hope that helps.
Related
This question already has answers here:
Why are all newlines gone after PowerShell's Get-Content, Regex, and Set-Content?
(4 answers)
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 2 years ago.
The goal is simple:
Take contents of text file
Search for pattern
Save search back to the file
For example, i want to find the first occurrence between # and ##. Following regex works perfectly (\#)(.*?)(?=\#{2}). It finds what I want. However, PowerShell removes all new line characters effectively changing the formatting. So, following input text
#
This
Is
My
Text
##
becomes this
# This Is My Text
How to preserve the formatting?
Here is my PowerShell script
param (
[string]$filename
)
$content = Get-Content -Path $filename
$output = $filename
$regex = [Regex]::new('(\#)(.*?)(?=\#{2})')
$matches = $regex.Matches($content)
Set-Content -Path $output $Matches[0]
This question already has answers here:
Quoting -replace & variables
(5 answers)
Closed 4 years ago.
I am trying to replace filenames in a given folder, but using regular expressions as a filter and in the new filenames, in PowerShell.
For example, a file name "CEX-13" should be renamed to "C-0013"; "CEX-14" should change to "C-0014", etc.
I have this, but I get an error that I cannot create a file that already exists.
My current code is:
foreach ($file in get-childitem | where-object {$_.name -match "^CEX-(\d\d)"})
{
rename-item $file -newname ($file.name -replace "^CEX-(\d\d)", "C-00$1")
}
Any help will be greatly appreciated.
You need the dollar in the replacement to get past the PowerShell variable expansion in strings, and stay as a dollar sign as it gets to the regex engine.
Currently "C-00$1" becomes "C-00" and all the files will get the same name.
You need to escape it with a backtick
"C-00`$1"
or use single quotes 'C-00$1'
This question already has answers here:
How to handle backslash character in PowerShell -replace string operations?
(2 answers)
Closed 6 years ago.
Folks
I am unable to replace the below string in PowerShell, initially I suspected it to be due to regexp and used [regex]::Escape() to resolve the \ to double slash however it still does not work. Can you please suggest?
PS C:\User>"C:clog" -replace "C:c" , ""
PS C:\User>log
PS C:\User>"C:\\c\\log" -replace "C:\\c\\", ""
PS C:\User>C:\\c\\log
Use Split-Path, it is designed for this:
Split-Path "C:\\c\\log" -Leaf
# returns "log"
This question already has answers here:
Perl regex: replace all backslashes with double-backslashes
(6 answers)
Closed 8 years ago.
How do I replace single backslashes in a string with double backslashes?
I've tried things such as
s/\\(?!\\)/\\\\/g
s/\\/\\\\/g
s/[^//]/\\\\/g
But they all produce multiple backslashes after each other.
So I want:
\test
to be replaced with
\\test
Edit: Sorry I should also mention that the regex is in a loop so I need a regex that only matches the string if there is ONLY ONE backslash. Once there is more than one backslash then the regex should reject the string. Apologies
The most helpful thing to note is to use a different delimiter for the regex, so things don't get jumbled by all the leaning towers:
my $str = '\test';
$str =~ s{\\}{\\\\}g;
print $str;
Outputs:
\\test
Update
Per your revised specification, if you only want to escape a single backslash, and ignore all others, then just use a negative lookahead and lookbehind assertion:
my $str = <<'END_STR';
\one \\two \\\three
END_STR
print $str;
$str =~ s{(?<!\\)\\(?!\\)}{\\\\}g;
print $str;
Outputs:
\one \\two \\\three
\\one \\two \\\three
echo '\replace' | perl -pe 's/\\/\\\\/g'
\\replace
OR with sed
# echo '\replace' | sed 's/\\/\\\\/g'
\\replace
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Perl: How do you insert numbers after grouping variable?
I have the following perl one liner:
perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/$123424977\t/g if $. <= 200'
The problem is that I want to insert the number 23424977 after the grouped regex (.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t).
But Perl thinks I'm referring to group $123424977 and doesn't recognize I mean $1 and that 23424977 is the number I want to insert afterwards. How can I correct this?
simply refer to the $1 capture group with surrounding braces: ${1}:
perl -pi.bak -e 's/(.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t.*?\t)/${1}23424977\t/g if $. <= 200'