Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 我怎样才能逃脱';powershell参数中的字符?_Regex_Git_Powershell_Windows Scripting - Fatal编程技术网

Regex 我怎样才能逃脱';powershell参数中的字符?

Regex 我怎样才能逃脱';powershell参数中的字符?,regex,git,powershell,windows-scripting,Regex,Git,Powershell,Windows Scripting,我使用的是一个powershell脚本,用于检查两个git分支之间的提交差异。 下面是我从git expression获取的信息: $result = Invoke-Expression "& git rev-list --left-right --count origin/$TargetBranch...origin/$SourceBranch 2>&1" 不幸的是,我发现当$SourceBranch包含'字符时,它会抛出一个 第1行[错误]字符:143 +…(移动)

我使用的是一个powershell脚本,用于检查两个git分支之间的提交差异。 下面是我从git expression获取的信息:

$result  = Invoke-Expression "& git rev-list --left-right --count origin/$TargetBranch...origin/$SourceBranch 2>&1"
不幸的是,我发现当
$SourceBranch
包含
'
字符时,它会抛出一个

第1行[错误]字符:143 +…(移动)到(录制)(图标)不(响应)2>&1

字符串缺少终止符:'

我已经实现了如下特殊字符的转义:
[System.Text.RegularExpressions.Regex]::Escape($SourceBranch)
然后
$SourceBranch.replace('\','`')

但是它不会转义
字符,将其放入
replace(“'”,“`')
中也不起作用


使用Powershell 5.0。

只需避免调用表达式即可:

$result = & git rev-list --left-right --count origin/$TargetBranch...origin/$SourceBranch 2>&1

您需要转义转义字符本身

在PowerShell中运行
a'b.bat
的示例

#创建包含以下内容的文件名'
echo“echo 1”|设置内容-编码Ascii“a'b.bat”
#双引号
调用表达式“\a``b.bat”
调用表达式“\a”“b.bat”
#单引号
调用表达式“.\a”“b.bat”
调用表达式“.\a”“b.bat”
#带-替换运算符
$source=“.\a'b.bat”
调用表达式“cmd/c$($source-replace“,”“”“)
这可能看起来很奇怪,但与
JSON.stringify({a:\\\})=={\'a\\:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

但是,如果不使用前面已经回答过的调用表达式,那么就容易多了

<代码>&“\a'b.bat” &“.\a”“b.bat”
尝试将符号加倍,
$SourceBranch-替换“'”,“'''”
请参阅有关powershell的信息