ack-grep:字符转义 [英] ack-grep: chars escaping
问题描述
我的目标是找到所有带有 ack 的<?=
".我该怎么做?
My goal is to find all "<?=
" occurrences with ack. How can I do that?
ack "<?="
不起作用.请告诉我如何解决这里的转义问题?
Doesn't work. Please tell me how can I fix escaping here?
推荐答案
由于 ack
使用 Perl 正则表达式,所以你的问题源于 Perl RegEx 语言中,?
是一个特殊字符,意思是最后一次匹配是可选的".所以你要搜索的是 =
前面是一个可选的 <
Since ack
uses Perl regular expressions, your problem stems from the fact that in Perl RegEx language, ?
is a special character meaning "last match is optional". So what you are grepping for is =
preceded by an optional <
所以你需要转义 ?
如果它只是一个普通字符.
So you need to escape the ?
if that's just meant to be a regular character.
要转义,有两种方法 - <?=
或 <[?]=
;有些人发现第二种转义形式(将特殊字符放入字符类)比反斜杠转义更易读.
To escape, there are two approaches - either <?=
or <[?]=
; some people find the second form of escaping (putting a special character into a character class) more readable than backslash-escape.
UPDATE 正如 Josh Kelley 在评论中慷慨地添加的那样,第三种转义形式是使用 Q
运算符,该运算符将以下所有特殊字符转义,直到 遇到E
,如下:Q=E
UPDATE As Josh Kelley graciously added in the comment, a third form of escaping is to use the Q
operator which escapes all the following special characters till E
is encountered, as follows: Q<?=E
这篇关于ack-grep:字符转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!