Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 选择两个字符之间的所有反斜杠_Regex_Powershell - Fatal编程技术网

Regex 选择两个字符之间的所有反斜杠

Regex 选择两个字符之间的所有反斜杠,regex,powershell,Regex,Powershell,我正在编写一个powershell脚本,我有几个文本文件需要替换与此模式匹配的行中的反斜杠:。>\\%名称%…

我正在编写一个powershell脚本,我有几个文本文件需要替换与此模式匹配的行中的反斜杠:
。>\\%名称%…<
可以是任何内容)

反斜杠应匹配的其中一个文件中的字符串示例:

<Tag>\\%name%\TST$\Program\1.0\000\Program.msi</Tag>
<Tag>/i /L*V "%TST%\filename.log" /quiet /norestart</Tag>
\\%name%\TST$\Program\1.0\000\Program.msi
反斜杠不应匹配的某个文件中的字符串示例:

<Tag>\\%name%\TST$\Program\1.0\000\Program.msi</Tag>
<Tag>/i /L*V "%TST%\filename.log" /quiet /norestart</Tag>
/i/L*V“%TST%\filename.log”/quiet/norestart

到目前为止,我已经成功地选择了
\\%name%
之间的每个字符,我建议选择带有的相关标记,然后在所选节点的文本体上进行替换

$xml.SelectNodes('//Tag[substring(., 1, 8) = "\\%name%"]' | ForEach-Object {
    $_.'#text' = $_.'#text' -replace '\\', '\\'
}
因此,我的解决方案如下:

$original_file = $Filepath
$destination_file =  $Filepath + ".new"

Get-Content -Path $original_file | ForEach-Object { 
    $line = $_
    if ($line -match '(?<=>\\\\%name%)(.*)(?=<)'){
       $line = $line -replace '\\','/'
     }
    $line
} | Set-Content -Path $destination_file
Remove-Item $original_file
Rename-Item $destination_file.ToString() $original_file.ToString()
$original\u file=$Filepath
$destination_file=$Filepath+“.new”
获取内容-路径$original_file | ForEach对象{
$line=$_

如果($line-match'(?\\\\%name%)(.*)(?=我不是100%,但这在正则表达式中可能不可能。您的问题与问题类似。我不知道如何使用组多次捕获每个反斜杠,而不捕获它们之间的文本。