12

Hi im new to ack (actually started half an hour ago). Im impressed by the quality of the search results.

But in my source files there are a lot of comments and if i search for a class/function-name i get about 20 results with commented lines and 2 with the actual code.

Is there a way to exclude text after // and # or between /* */?

Shylux
  • 1,163
  • 2
  • 16
  • 31
  • Do you find more informations about this now ? – smonff Oct 27 '13 at 20:20
  • 2
    Maybe [this thread on ack-users mailing list could interest you](https://groups.google.com/forum/#!topic/ack-users/sfNos0L7oOU)... But it says it's not possible, and explain why it won't. – smonff Oct 28 '13 at 14:11

2 Answers2

3

You could skip single line comments with something like this;

# ignore matches after //
ack '^[^//]*word'
# ignore matches after #
ack '^[^#]*word'

If you are using ag instead, remember that it does multi-line matches:

# ignore matches after //
ag '^[^\n\r//]*word'
# ignore matches after #
ag '^[^\n\r#]*word'
sina
  • 960
  • 2
  • 8
  • 20
0

You don't. Instead, you tag your source.

Skipping comments would mean parsing the input on the fly, which is generally speaking slow. Moreover, each programming language has a different syntax for comments, which makes implementing this non-trivial. While you could roll your own regex -- e.g. for C use something like this: \/\*(\*(?!\/)|[^*])*\*\/ -- these patterns are complex and more trouble than their worth for the quick searching that ack, ag, and grep are designed for. What you're really looking is a way to navigate your code via functions and classes; this is what tagging one's code is for: check out universal-ctags, http://docs.ctags.io/en/latest/.

gregory
  • 10,969
  • 2
  • 30
  • 42