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).
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.
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 expression
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.
|
|
|
|
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
|
|
matches the empty string,
but only at the beginning of the line
|
^foo
matches foo and food, but not ofoo
|
|
matches the empty string,
but only at the end of the line
|
fo$
matches a string ending in fo, but not foop
|
|
escapes special characters
(including \)
|
|
|
|
foo|bar
matches either foo or bar
|
|
|
foo(bar)*
matches zero or more instances of bar with foo (such as foo,
foobar, and foobarbar)
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
characters that are printable
and are also visible
(a tab is printable, but not visible, while an a is both) |
|
|
lower-case alphabetic
characters
|
|
|
printable characters
(ASCII 32 and above), but not
control characters
|
|
|
|
|
|
space characters (such
as space, tab, newline, and
page eject)
|
|
|
upper-case alphabetic
characters
|
|
|
hexadecimal digit characters
|
|
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:
|
|
|
|
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)
|
|
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)
|
|
post-fix operator that
must match an expression once or not at all
|
f?o
matches fo or o, nothing else
|
|
preceding item is matched
exactly n times
|
|
|
proceeding item is matches
n or more times
|
fo{2,}
matches foo and fooo
|
|
proceeding item is optional
and is matched at most m times
|
fo{,3}
matches f, fo, foo, and fooo
|
|
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.
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.