[英]Powershell - using variables in replace
I was using .replace until I discovered it is case sensitive. 我一直使用.replace,直到发现它区分大小写。 So I am rewritng a line of code to use -replace instead.
因此,我重新编写了一行代码以使用-replace代替。
Here is what is working, but is case sensitive: 这是起作用的,但是区分大小写:
$SourcePath = 'c:\scripts\test
$folder = 'c:\testing\test'
$sourceFullPath = 'c:\scripts\test\FolderToTest'
$sourceFileRelativePath = $sourceFullPath.Replace($SourcePath, "")
$destFullFilePath = $folder + $sourceFileRelativePath
Write-output $destFullFilePath
c:\testing\test\FolderToTest
How would I convert this to use -replace or is there a way to use the .net .replace case-insensitive? 我如何将其转换为使用-replace或有没有办法使用.net .replace不区分大小写?
Note: This section of code will be in a function so they will not be static. 注意:这部分代码将在函数中,因此它们不是静态的。 I put in examples for this post but they could be any file path.
我在这篇文章中放了一些示例,但它们可以是任何文件路径。
Thanks!! 谢谢!!
Unlike the Replace method which takes strings, the replace operator takes a regular expression pattern. 与使用字符串的Replace方法不同,replace运算符采用正则表达式模式。 $SourcePath needs to be escaped as it contains backslashes which are special regex characters.
$ SourcePath需要转义,因为它包含反斜杠,这是特殊的正则表达式字符。
$sourceFileRelativePath = $sourceFullPath -replace [regex]::escape($SourcePath)
$destFullFilePath = Join-Path $folder $sourceFileRelativePath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.