Search Through or Skip Source Code Comments and Strings

When searching through or modifying source code files, you'll often want to restrict the search to comments and/or strings, or search through comments and/or strings exclusively. E.g. if you discover you've been misspelling "referrer" as "referer" throughout your project, you'd probably want to fix the mistake in comments and strings, but leave the actual source code untouched. Modifying the source code might break ties to other modules, a hassle not worth correcting a spelling mistake. (As a bit of trivia: the Apache web server stores the referring URL in a variable HTTP_REFERER for exactly this reason.)

PowerGREP makes this easy with the "file sectioning" part of the action definition. The examples below only describe the file sectioning settings. Enter the actual search terms in the as usual.

Search Through Comments and Strings Only

  1. Select files and set the main part of the action as usual.
  2. Select "search for sections" from the "file sectioning" list.
  3. Set the section search type to "list of regular expressions". Make sure "non-overlapping search" is on.
  4. Add one regular expression to the list for each kind of string and comment the programming language you're working with supports. E.g. for C or Java, use //.* for single-line comments, (?s)/\*.*?\*/ for multi-line comments, and "[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*" for strings. The (?s) in the second regex turns on "dot matches newlines" for that regular expression only. Make sure the checkbox is not checked.

Don't Search Through Either Comments or Strings

Searching through source code only, skipping comments and strings, is just as easy. Instead of selecting "search for sections" in step 2 above, select "split along delimiters" instead.

"Split along delimiters" means that PowerGREP will treat comments and strings as delimiters. PowerGREP will make the main action search through everything between comments and strings, skipping the comments and strings themselves.

Search Through Comments Only, or Strings Only

You might be tempted to clear the checkboxes in front of the regular expressions in the file sectioning that match the parts of the file you don't want to search through. But that won't have the effect you intended.

Unticking a checkbox disables that regular expression completely. This is be useful when you're testing the effect of different regular expressions while designing a PowerGREP action. That is not what you want in this situation. E.g. if you disable the regexes for matching comments, the string regex will match strings in commented-out code.

To skip certain sections, select "search and collect sections" from the "file sectioning" list. A new Section Collect box will appear. In this box, enter \0 for each sectioning step that the main action should search through. Leave it blank for sections that should be skipped.

\0 is a backreference to the entire regular expression match. When using your own regular expressions to section files, you can also use backreferences to capturing groups in the regular expression. Then PowerGREP will restrict the main part of the action to the part of the file matched by that capturing group.