ack-grep:字符逃跑 [英] ack-grep: chars escaping

查看:92
本文介绍了ack-grep:字符逃跑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是用ack找到所有<?= 出现次数。我怎么能这样做?

  ack<?=

不起作用。请告诉我如何在这里修改转义?

解决方案

由于 ack 使用Perl正则表达式,你的问题源于这样一个事实:在Perl正则表达式语言中,是一个特殊字符,意思是最后匹配是可选的。因此,你在寻找的是 = ,后面跟着一个可选的<



所以你需要转义,如果这只是一个普通的字符。 $ b

为了逃避,有两种方法 - < \?= 百分比抑制率数据?] = ;有些人发现第二种转义形式(将特殊字符放入字符类中)比反斜杠转义更具可读性。


$ b

更新由于乔希Kelley慷慨地添加到注释中,第三种转义形式是使用 \ Q 运算符,它会转义所有以下特殊字符,直到 \E遇到,如下所示: \ Q<?= \ E


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?

解决方案

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 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屋!

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