有没有一种方法可以在ripgrep中为MS Windows(Powershell或CMD)转义引号? [英] Is there a way to escape quotes in ripgrep for MS Windows (Powershell or CMD)?

查看:105
本文介绍了有没有一种方法可以在ripgrep中为MS Windows(Powershell或CMD)转义引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to find a string "Hello (Hello starts with double quote) in text files using ripgrep.

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

Normally, in Bash or ZSH, this would work by escaping with backslash or surrounding with single quote:

rg \"Hello
rg '"Hello'

但是,在MS Windows(Powershell和CMD)中,我已经尝试了这些,但是这些都不起作用:

However, in MS Windows (Powershell and CMD), I've tried these but none of these worked:

rg \"Hello
rg '"Hello'
rg `"Hello
rg '`"Hello'

在MS Windows中,有什么方法可以使用ripgrep来对单引号或双引号进行转义吗?

Is there any way to escape single or double quotes using ripgrep in MS Windows?

推荐答案

逐字字符串"Hello 最终必须作为 \" Hello 传递给 rg ("\" Hello"也可以).即,逐字记录" char.必须 \ -转义:

Verbatim string "Hello must ultimately be passed as \"Hello to rg ("\"Hello" would work too). That is, the verbatim " char. must be \-escaped:

来自 cmd.exe :

From cmd.exe:

rg \^"Hello

^ cmd.exe 的转义字符,确保对"进行逐字处理并由 cmd删除.exe ,然后再调用 rg .
请注意,这里并非强制使用 ^ ,但可以防止将"视为双引号参数的开头,如果有其他参数,则可能会有所不同争论.

^, cmd.exe's escape character, ensures that the " is treated verbatim and is removed by cmd.exe before calling rg.
Note that ^ isn't strictly necessary here, but it prevents the " from being considered the start of a double-quoted argument, which could make a difference if there were additional arguments.

来自 PowerShell :

rg \`"Hello

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

`, PowerShell's escape character, ensures that the " is treated verbatim and is removed by PowerShell before calling rg.

可以说,不需要显式的 \ 转义,因为在用户满意之后,shell的职责是正确地将参数传递给目标可执行文件外壳自身的转义要求(用 cmd.exe 中的 ^ 以及在PowerShell中使用`转义逐字").

Arguably, the explicit \-escaping shouldn't be necessary, because it is the duty of a shell to properly pass arguments to target executables after the user has satisfied the shell's own escaping requirements (escaping the verbatim " with ^ in cmd.exe, and with ` in PowerShell).

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

In the context of PowerShell, this problematic behavior is summarized in this answer.

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

Note that in PowerShell this extra escaping is only needed if you call external programs; it isn't needed PowerShell-internally - such as when you call Select-String, as shown in js2010's answer.

这篇关于有没有一种方法可以在ripgrep中为MS Windows(Powershell或CMD)转义引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆