有没有办法在 ripgrep 中为 MS Windows(Powershell 或 CMD)转义引号?

Dan*_*Kim 5 powershell cmd ripgrep

我想"Hello在文本文件中找到一个字符串(Hello 以双引号开头)ripgrep

通常,在 Bash 或 ZSH 中,这可以通过使用反斜杠转义或用单引号括起来来实现:

rg \"Hello
rg '"Hello'
Run Code Online (Sandbox Code Playgroud)

但是,在 MS Windows(Powershell 和 CMD)中,我尝试过这些,但没有一个奏效:

rg \"Hello
rg '"Hello'
rg `"Hello
rg '`"Hello'
Run Code Online (Sandbox Code Playgroud)

有没有办法在 MS Windows 中使用 ripgrep 转义单引号或双引号?

mkl*_*nt0 5

逐字字符串"Hello最终必须传递给\"Hellorg"\"Hello"可以)。即逐字"字符。必须\转义

cmd.exe

rg \^"Hello
Run Code Online (Sandbox Code Playgroud)

^,cmd.exe的转义字符,确保"逐字处理 并在cmd.exe调用 之前将其删除rg
请注意,^这里并不是绝对必要的,但它可以防止"被视为双引号参数的开头,如果有其他参数,这可能会产生影响。

来自PowerShell

rg \`"Hello
Run Code Online (Sandbox Code Playgroud)

`,PowerShell 的转义字符,确保"逐字处理 ,并在调用 之前被 PowerShell 删除rg


可以说,显式\转义应该不是必需的,因为在用户满足 shell 自己的转义要求(在 PowerShell 中使用 in 和 with 逐字转义)"^cmd.exeshell有责任正确地将参数传递给目标可执行文件。`

在 PowerShell 的上下文中,此答案总结了这种有问题的行为。

请注意,在 PowerShell 中,仅当您调用外部程序时才需要这种额外的转义;它不需要PowerShell-内部-例如当你调用时,如js2010的答案Select-String所示。