Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Hey, Check Out My Slick New Editor! : Page 2

As a developer, you presumably spend much of your time working in a code editor—so you owe it to yourself to explore what a truly full-featured editor can offer.


advertisement

Regular Expression Search and Replace

This isn't an exclusive SlickEdit feature, some other editors have it too; but if you haven't had the opportunity to experience regular expression searches, you should really consider switching to an editor that supports the feature.

Have you ever had to change an API in a product? Not just a name change, but a calling convention change, where you have to switch or add parameters. Such change requirements can be major problems when you need to make many changes or make them in many files. In such cases, you'll find a regular expression search/replace feature invaluable.



Many developers are familiar with using regular expressions to match patterns. At a simple level, dir *.c is a great example of one you may have used. However, you haven't really explored the power of regular expressions until you've used them in a replacement pattern. Using this technique, you can define a pattern, find a match, and then use part of the pattern you've designated in the replacement!

 
Figure 2. Regular Expression Evaluator: This tool helps you build regular expressions interactively.

Here's an example. You need to change a function, and so of course you also need to alter calls to that function wherever they reside in a program. This example requires changing some code that uses a less secure version of a function to a more secure equivalent. You'll see that it's quite easy using a regular expression replace operation.

At this point, you may be thinking, "But I don't understand regular expressions." That's OK; Visual SlickEdit provides several tools that can help you build appropriate regular expressions. First, just to the right of the find and replace fields, you'll see an arrow that—when you click it—brings up an extensive list of common expressions: everything from "match any character" to "beginning of line." Because you also have a choice of regular expression syntax (Unix, Perl, Brief), SlickEdit can even help you understand the differences between the expression syntax in each language.

Taking help a step further, SlickEdit has a regular expression evaluator available from the Tools menu (see Figure 2).

If you open this up, you'll find an awesome tool that helps you figure out whether you have the right syntax for a match! The regular expression evaluator is a powerful recent addition to SlickEdit. I was able to paste in some sample patterns I wanted to match in my program and then edit the regular expression until I got them to light up (see Figure 3).

 
Figure 3. Successful Pattern Match: When evaluating regular expressions, a successful test pattern match makes the pattern "light up."

If you look carefully at the regular expression syntax at the bottom of the RegEx Evaluator dialog in Figure 3, you'll see two "extra" parentheses around the strcat parameters. These tell SlickEdit to "remember" these matches for later. You'll see how that works in a second.

Having created a valid and tested pattern, it's time to do the actual find and replace. From the Edit menu select Replace (or F6 if you have Brief emulation turned on) and start typing your expression into the Find window. The example shown in Figure 4 is asking for patterns that match strcat followed by an opening parenthesis, then a group of one or more characters followed by a comma, and then another group of one or more characters followed by a closing parenthesis.

 
Figure 4. Search/Replace: This search replaces calls to the strcat function with calls to strncat.

 
Figure 5. Replace Prompt: SlickEdit's regular expression search matched this call to strcat and shows how the replacement would look if you accept it.

Next, set the replacement string. In this case, you want to replace strcat with strncat—a more secure function that honors the size of the buffer you are copying into. (There are other, possibly better, safe string function options, but this one exists in all common C runtime libraries that I'm aware of.) Note the \1, which means "substitute the first grouping found during the search." The \2, of course, means "substitute the second grouping found during the search." Finally, you need to add the size of the buffer as a third parameter. When you've completed the dialog, click Replace, and for each match, you'll see something similar to Figure 5.

For each match, you can determine whether you want to replace that occurrence and go onto the next. Click the Last button if this is the last replacement you want to make. The Go button functions as a "Replace All" feature, making the replacement for all available matches. With some practice, you'll find yourself pulling this regular expression search/replace feature out of your tool belt more and more often.

Author's Note: A little forethought is warranted if you plan to make several broad changes that may be interdependent. Sometimes such changes are easier if you make them in pieces: first replacing code with something easy to find, and then performing a second find/replace to make the final changes. If something shows up in your current replace string that you anticipate changing later, you may want to reverse the order and perform the other replace operation first.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap