ack-grep: chars escaping

自闭症网瘾萝莉.ら 提交于 2019-12-18 14:09:08

问题


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?


回答1:


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




回答2:


Rather than trying to remember which characters have to be escaped, you can use -Q to quote everything that needs to be quoted.




回答3:


ack -Q "<?="

This is the best solution if you will want to find by simple text.

(if you need not find by regular expression.)




回答4:


ack "<\?="

? is a regex operator, so it needs escaping



来源:https://stackoverflow.com/questions/3074122/ack-grep-chars-escaping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!