Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ack-grep: chars escaping

Tags:

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?

like image 809
user80805 Avatar asked Jun 19 '10 02:06

user80805


People also ask

What characters need to be escaped grep?

Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).

Is ACK better than grep?

ack is easy to use: the main complication/feature relative to grep is that it uses perl regular expressions instead of POSIX ones. Another difference is that, while grep is an excellent general purpose tool, ack is specialized to serve the needs of programmers.

What is escaped character in regex?

? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters. Use a double backslash ( \\ ) to denote an escaped string literal.


1 Answers

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

like image 116
DVK Avatar answered Nov 08 '22 00:11

DVK