# Grep

Source-Navigator's Grep tool allows you to search for text patterns in source files throughout the project. It is more powerful than using `grep` on the command line because it:

- supports predefined regular expressions.
- provides easy entry of filter patterns and search constraints.
- allows you to search multiple directories.
- searches only the files in your project.
- can be restricted to a subset of the files in your project.

## Using Grep

Start the Grep tool by selecting Windows -> New View -> Grep-Editor menu. This opens a Grep/Editor split window (for more information on split windows, see Adding a Browser/Navigator to an Existing Window).

#### The Grep/Editor Window

In the Pattern text box, enter a regular expression then click Search. For more information on regular expressions, see GNU Regular Expressions.

You can use the Files filter to limit your search; for example, entering `*.cc` restricts the search to only C files. For a list of file extensions and their associated languages see Table 2. Sample Grep Search Results shows the results of a sample Grep search.

#### Sample Grep Search Results

Clicking on an item in the Grep results list displays the appropriate file in the Editor, with the cursor positioned at the selected line. To scroll through the Grep results, click the left or right black arrow keys in the toolbar. To filter your Grep results, use the Format combo-box to select an option.

## GNU Regular Expressions

A GNU regular expression1 is a pattern that describes a set of strings. Regular expressions are constructed like arithmetic expressions: various operators combine smaller expressions to form larger expressions.

### Ordinary Characters

An ordinary character is a simple regular expression that matches a single character and nothing else. For example, f matches f and no other string. You can concatenate regular expressions together. For example, foo matches foo and no other string.

### Special Characters

You can combine regular expressions with regular expression operators, or metacharacters, to increase the versatility of regular expressions. Traditional expression characters are enclosed by brackets [ and ].

For example, [Aa]pple matches either Apple or apple. A range of characters is indicated by a dash -. [a-z] matches any lowercase letter and [0-9] matches any digit string between 0 and 9. You can string groups together, so that [a-zA-Z0-9] matches any single alphanumeric character.

If ^ follows the initial [ the set is negated, such that all characters not in the set are accepted.

To represent the - dash itself, it must be the last character, directly succeeded by the ]. To represent ] bracket, it must be the first character after the [ or the [^.

Special Characters lists special characters and examples of how to use them.

Special Characters
Symbol | Definition | Example
. (period) | matches any single character except a new line | a.b matches any three-character string beginning with a and ending with b (such as acb, a6b, a#b)
[... ] | matches characters between the brackets. | [af] matches either one a or one f [af]* matches any string composed of just a's or f's or the empty string
[^...] | matches any character except the ones specified | [^a-z] matches any characters except lower-case letters
^ (caret) | matches the empty string, but only at the beginning of the line | ^foo matches foo and food, but not ofoo
$ (dollar sign) | matches the empty string, but only at the end of the line | fo$ matches a string ending in fo, but not foop
\ (backslash) | escapes special characters (including \) | fo\? matches fo?
| (pipe) | is used to designate OR | foo|bar matches either foo or bar
(...) | is a grouping construct | foo(bar)* matches zero or more instances of bar with foo (such as foo, foobar, and foobarbar)

Note: | In the | (pipe) example above, notice that foo|bar is matching either foo or bar. It's not matching o or b, resulting with either fooar or fobar.

### Predefined Sets of Characters

Certain named classes of characters are predefined, but can only be used within bracket expressions.

Character Classes
Symbol | Definition | C-locale Equivalent
[:alnum:] | alphanumeric characters | [a-zA-Z0-9]
[:alpha:] | alphabetic characters | [a-zA-Z]
[:blank:] | space and tab characters |
[:cntrl:] | control characters |
[:digit:] | numeric characters | [0-9]
[:graph:] | characters that are printable and are also visible (a tab is printable, but not visible, while an a is both) |
[:lower:] | lower-case alphabetic characters | [a-z]
[:print:] | printable characters (ASCII 32 and above), but not control characters |
[:punct:] | punctuation characters |
[:space:] | space characters (such as space, tab, newline, and page eject) |
[:upper:] | upper-case alphabetic characters | [A-Z]
[:xdigit:] | hexadecimal digit characters | [0-9a-fA-F]

For example, [[:alnum:]] means [0-9A-Za-z] in the C-locale, except the latter form is dependent upon the ASCII character encoding, whereas the former is portable.

### Repetition

A regular expression matching a single character may be followed by one of several repetition operator:

Interval Expressions
Symbol | Description | Example
* (asterisk) | post-fix operator that matches an expression 0 or more times | fo* matches a string starting with f and ending with a repeating o or no o's (such as f, fo, foo)
+ (plus) | post-fix operator that matches an expression at least once | f+o matches a string starting with one or more f's and ending with an o (such as fo, ffo, and fffo)
? (question mark) | post-fix operator that must match an expression once or not at all | f?o matches fo or o, nothing else
{n} | preceding item is matched exactly n times | fo{2} matches foo
{n,} | proceeding item is matches n or more times | fo{2,} matches foo and fooo
{,m} | proceeding item is optional and is matched at most m times | fo{,3} matches f, fo, foo, and fooo
{n,m} | proceeding item is matched at least n times and at most m times | fo{1,3} matches fo, foo, and fooo

### Escape Sequences

Some characters cannot be included literally in regular expressions. You represent them instead with escape sequences, which are characters beginning with a backslash \. A backslash represents unprintable characters such as a tab or newline.

Escape Sequences
Symbol | Description
\\ | a literal backslash
\a | alert
\f | formfeed
\n | newline
\r | return
\t | horizontal tab
\v | vertical tab
\? | question mark
\( | left parenthesis
\) | right parenthesis
\[ | left bracket
\] | right bracket

Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated subexpression.

The backreference \n, where n is a single digit, matches the substring previously matched by the nth parenthesized subexpression of the regular expression.

1. Richard Stallman and Karl Berry wrote the GNU regex backtracking matcher. Copyright (C) 1989, 1991 Free Software Foundation, Inc., 675 Massachusetts Avenue, Cambridge, MA 02139, USA.