#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;
}
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.
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.
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.
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;
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.
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.
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.
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.
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]+);
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.