[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


Find and Replace Using Regular Expressions

You can create regular expressions to find and replace text in your files. The IDE uses the regular expression to match text in the active files. This section discusses the following topics:

Listing 6.1 shows sample code. Each topic in this section refers to this sample code.

Example Code:
#include <iostream>

#define  var1  10;
#define  var2  20;

using namespace std;

int result = 0;

int main(void)
{
	cout << "This example provides information about" << endl;
	cout << "the use of regular expressions in the" << endl;
	cout << "CodeWarrior IDE." << endl << endl;
	cout << "Refer to this code when reading the" << endl;
	cout << "sections in the manual dealing with" << endl;
	cout << "the use of regular expressions." << endl << endl;
	cout << "The value of var1 is " << var1;
	cout << " out of 50, and" << endl;
	cout << "the value of var2 is " << var2;
	cout << " out of 50." << endl;
	cout << "$" << var1;
	cout << " + $" << var2;
	cout << " = $" <<;
	result += var1;
	result += var2;
	cout << result;
	cout << endl << endl;
	return 0;
}


Matching simple expressions

Most characters match themselves. For example, x matches all occurrences of the letter x in the code. The only exceptions are called operators: the asterisk (*), plus sign (+), backslash (\), period (.), caret (^), square brackets ([ and ]), dollar sign ($), and ampersand (&). To match an operator, precede it with a backslash, like this: \* You can also turn any operator (except the caret) into a literal by enclosing it in square brackets.

For example, refer to the example code shown in Listing 6.1. If you want to find every occurrence of the letter m in the code, you would type m into the Find field. The IDE matches this regular expression with the m in iostream. Subsequent searches match the m in namespace, main(void), example, information, and manual.

If you want to find every occurrence of a dollar sign in the code, you would type \$ in the Find field. The backslash tells the IDE to interpret the dollar sign as a normal character instead of an operator. The IDE matches the regular expression with the first dollar sign in the following line from Listing 6.1:


cout << "$" << var1;

Subsequent searches match the second and third dollar signs in the next two lines.


Matching any character

A period (.) matches any character except a newline character. Use the period when you want to be more flexible in your search.

For example, refer to the example code shown in Listing 6.1. If you want to find four-character expressions in the code that begin with var, enter the expression var. in the Find field. The period following the var tells the IDE to look through the code for var immediately followed by a single character. The IDE matches this regular expression with var1 and var2 in the code.


Repeating expressions

The asterisk (*) and plus sign (+) are two special operators that allow you to "repeat" expressions in your search string.

For example, refer to the example code shown in Listing 6.1. Suppose you type s*ion in the Find field. The s* tells the IDE to match zero or more occurrences of the letter s just before the occurrence of ion. In Listing 6.1, this regular expression matches the ion in both information and sections from the following lines:


cout << "This example provides information about" << endl;
cout << "sections in the manual dealing with" << endl;

The same regular expression also matches the ssion in expressions from the following lines:


cout << "the use of regular expressions in the" << endl;
cout << "the use of regular expressions." << endl << endl;

If you want to find matches that have at least one letter s preceding ion, you would type s+ion in the Find field. The plus sign tells the IDE to match at least one occurrence of the letter s just before the occurrence of ion. This regular expression matches the ssion in expressions from Listing 6.1. Notice that s+ion does not match the ion in information or sections, since there is not at least one letter s preceding ion in either case.

If you want to find expressions in the code that contain the number zero, followed by one period or no periods at all, you would type 0\.? in the Find field. The backslash tells the IDE to treat the period as a normal character, and the ? special operator acts on the normal period character. This regular expression matches each occurrence of the number zero in Listing 6.1. This regular expression also matches the 0. from the following line:


cout << " out of 50." << endl;

As shown in these examples, the asterisk and plus sign usually refer to a single character. However, it is possible to refer to more than one character at a time by grouping expressions. See "Grouping expressions" for more information.


Grouping expressions

If you enclose an expression in parentheses (( and )), the editor treats the expression as a single unit and applies any asterisk (*) or plus (+) operator to the whole expression.

For example, refer to the example code shown in Listing 6.1. If you want to find expressions in the code that match is, you could simply type is in the Find field. However, you could also use ( i)s as the regular expression. Notice that ( i)s tells the IDE to look for the letter s preceded by both a space and the letter i. Whereas is matches the is within This, this, and is, ( i)s matches only is in the following two lines from Listing 6.1:


cout << "The value of var1 is " << var1;
cout << "The value of var2 is " << var2;


Choosing one character from many

A string of characters enclosed in square brackets ([]) matches any one character in that string. To match any character that is not in the string enclosed within the square brackets, precede the enclosed expression with a caret (^) like this: [^abc]

For example, refer to the example code shown in Listing 6.1. If you want to find expressions in the code that contain the letters x, y, or z, you would type [xyz] in the Find field. The IDE matches this regular expression with the letter x in example and expressions. If instead you want to find expressions in the code that do not contain the letters x, y, or z, type [^xyz] in the Find field. The IDE matches this regular expression with every character in the example code except the letter x in example and expressions.

Placing a minus sign (-) within square brackets indicates a range of consecutive ASCII characters. For example, [0-9] is the same as [0123456789]. Placing the minus sign as the first or last character within the enclosed string causes the minus sign to loses its special meaning and become an ordinary character. For example, [-bc] represents the minus sign and the letters b and c.

If a right square bracket immediately follows a left square bracket, it does not terminate the string but instead becomes one of the characters to match. For example, []0-9] tells the IDE to search for the right square bracket as well as any digit. If any special character, such as backslash (\), asterisk (*), or plus sign (+), immediately follows the left square bracket, that special character becomes an ordinary character. For example, [.] tells the IDE to search for periods.

You can use square brackets in a similar manner as parentheses. The IDE treats the information in the square brackets as a single unit. For example, [bsl]ag tells the IDE to search for occurrences of bag, sag, or lag. Typing [aeiou][0-9] in the Find field tells the IDE to search for a vowel followed by a number, such as a1.


Matching the beginning or end of a line

You can specify that a regular expression match only the beginning or end of a line.

For example, refer to the example code shown in Listing 6.1. If you want to find expressions in the code that match cout and occur only at the beginning of a line, you would type ^([ \t]*cout) in the Find field. The [ \t]* in the regular expression allows zero or more spaces and tabs to precede cout, as is the case with the code shown in Listing 6.1.


Using the Find string in the Replace string

You can include the contents of the Find string in the Replace string by using an ampersand (&) in the Replace string. For example, refer to the example code shown in Listing 6.1. Suppose the Find string is var[0-9] and the Replace string is my_&. The IDE matches the Find string with var1 and var2. Clicking the Replace button substitutes var1 with my_var1 or var2 with my_var2.

To use an ampersand in the Replace string without any special meaning, use \&. An ampersand has no special meaning in the Find string.


Remembering sub-expressions

You can remember sub-expressions of a regular expression in a Find string and recall those sub-expressions in the Replace string. You can create up to nine remembered sub-expressions for each Find string. You must enclose each sub-expression within parentheses. To recall these sub-expressions when typing the Replace string, use \n, where n is a digit that specifies which sub-expression to recall. Determine n by counting sub-expressions from the left side of the Find string.

For example, refer to the example code shown in Listing 6.1. If you want to change #define declarations into const declarations, you could perform a search for #define and manually change each occurrence to a const declaration. However, you can take advantage of remembered sub-expressions to perform the same process automatically. Begin by typing the following regular expression in the Find field:


\#define[ \t]+(.+)[ \t]+([0-9]+);

This regular expression tells the IDE to search for the following string, in the exact order given: #define, one or more spaces or tabs, one or more characters, one or more spaces or tabs, one or more digits, and a semicolon. Starting from the left side of the regular expression, the first sub-expression is (.+) and the second sub-expression is ([0-9]+). The Replace string recalls these two sub-expressions:


const int \1 = \2;

The \1 refers to the first sub-expression from the Find string and the \2 refers to the second sub-expression from the Find string. These two sub-expressions recall the variable name and its value from the original #define declaration. Notice that the Replace string changes the #define declaration into a const declaration by using references to the two sub-expressions. Thus, the editor finds #define var1 10; and changes it into const var1 = 10; in the code shown in Listing 6.1. It changes the next #define statement in the same manner.


References

For a comprehensive book on using regular expressions, refer to Mastering Regular Expressions, by Jeffrey E.F. Friedl, published by O'Reilly & Associates, Inc.


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 17, 2000