Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Inside Microsoft® Visual Studio® .NET 2003 [Electronic resources] - نسخه متنی

Brian Johnson

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید










Search, Replace, and Regular Expressions


If you can't easily search for and replace text in your editor, you're probably not working with a very good editor. It's great to be able to type in code easily, but finding and fixing code problems is something you must do often as a programmer, and the search functions built into the editor are what make that work easy (or difficult). Visual Studio .NET offers a number of ways to search in the Text Editor, and it offers a powerful regular expressions facility that allows you to do extremely complex searches.

First let's take a look at the named commands and shortcuts associated with the Find and Replace operations in Visual Studio .NET. These might be familiar to you because they're mapped to the Common User Accessibility (CUA) shortcuts that you might have used in Windows or in Office.

To bring up the Find dialog box, press Ctrl+F. You can see this dialog box in Figure 3-20. The options in this dialog box are fairly straightforward. You can specify case (Match Case), whole word searches (Match Whole Word), and the direction of your search (Search Up). An interesting option on this dialog box is Search Hidden Text. When this check box is left clear, text that is hidden in a collapsed node of an outline won't be searched.


Figure 3-20. The Find dialog box



The Find dialog box in Visual Studio .NET is actually a tool window, so you can dock it in the IDE or even toggle off the Dockable option on the shortcut menu available from the title bar to make it a tabbed window in the center of the IDE.

You can dock the Find dialog box by dragging it to a side of the IDE. But making it a floating dialog box might be preferable because it's easy to accidentally dock the window when you're trying to get it out of your way. You can turn off docking by choosing Floating on the shortcut menu for the dialog box. (Right-click the Close button on a dialog box to open its shortcut menu.) Doing so will let you drag the Find dialog box around the screen with impunity.

You can bring up the Replace dialog box by pressing Ctrl+H. This dialog box (shown in Figure 3-21) is nearly identical to the Find dialog box, except that it contains a combo box for the Replace With text.


Figure 3-21. The Replace dialog box



Using the Find and Replace dialog boxes is straightforward. Especially handy are the Search options, which let you choose between the current document, all open documents, the current project, and specific selections or blocks in the editor.

The Find In Files dialog box (Ctrl+Shift+F), shown in Figure 3-22, and the Replace In Files dialog box (Ctrl+Shift+H) make it fairly easy to find text within a project or a directory structure. Setting up these file searches takes a little more work than performing a standard find command. You might find this facility easier to use than the Windows search facility, but it's usually not quite as fast. (It depends on how wide your search is.) The most important button in this dialog box is the … button, which opens the Look In dialog box. You can use this dialog box to narrow your search to your project or to a directory structure on your machine. The output from the Find In Files search is sent to the Find Results window by default.


Figure 3-22. The Find In Files dialog box



The Visual Studio .NET Find and Replace shortcuts are listed in Table 3-6.

Table 3-6. Common Search Shortcuts

Command


Keystroke


Named Command


Find


Ctrl+F


Edit.Find


Replace


Ctrl+H


Edit.Replace


Find in files


Ctrl+Shift+F


Edit.FindinFiles


Replace in files


Ctrl+Shift+H


File.ReplaceinFiles


Wildcards


The Use check box in the Find and Replace dialog boxes lets you choose between using regular expressions in your search strings and using wildcard characters. If you're not used to regular expressions, the Wildcards option can provide you with a lot of extra functionality with a very short learning curve. The wildcard characters let you fine-tune your search parameters.

Let's take a look at how this works. If you want to search for a pattern of characters in a file that contains a number if incremented values such as H03XA01, H03XB02, H03XC03, H03XD04, and so on, you can use the # wildcard character to match any digit that comes up in the pattern and use the ? to match any character. To perform a search for these values, select Use in the Find dialog box and select Wild Cards from the adjacent combo box. The search string would then be something like H03X?0#.

The simplest wildcard search allows you to search for a match while disregarding an ending character or group of characters. The * wildcard character provides this functionality. Continuing with the example in the previous paragraph, a more general search string that matches the same pattern might actually be H03X*. If the body of the pattern isn't widely used in a file, you can expand the pattern to make things a little easier.

Be aware that searches using the * character can extend beyond the end of the term(s) you're searching on, so you can end up with more selected than you had intended. For example, if you're looking for the term class in the Code Editor and you're using cla* as your search term, one of the returned selections might be class Class1. When you're performing a replace operation, you're probably not looking for a string like this. If you know the pattern of the item you're searching for, it's best to use specific parameters when possible.

To search for a string that does not contain certain values, you use the [! ] wildcard. For example, working with the same list, let's say you want to match all the characters in the H03X?0# series except those that contain A or C as the fifth letter in the series. In such a case, the search string H03X[!AC]0# will return the values you're searching for while filtering out the items you don't want to see.

The [ ] wildcard works by matching any of the characters inside the brackets as part of the search. To return only the items that contain A and D, the search string would look like H03X[AD]0#.

Wildcards aren't as powerful as the regular expressions that we'll talk about in the next section, but they can be a lot easier to remember if you don't use regular expressions that often. You'll find the list of wildcard characters supported in Visual Studio .NET Find and Replace operations in Table 3-7.

Table 3-7. Wildcard Characters Supported in
Visual Studio .NET

Character


Description


?


Searches for any single character in the pattern


#


Searches for any single digit in the pattern


*


Searches for one or more matching characters in the pattern


[! ]


Searches for characters that do not match the items in this list


[ ]


Searches for characters that match the characters in this list


Regular Expressions


Whole books have been written about regular expressions, and it would take at least a chapter to do the subject justice, but because this is a book about Visual Studio .NET, we'll explain just enough about regular expressions in Find and Replace operations to get you started. Visual Studio .NET supports about 70 regular expressions that you can use in Find and Replace operations. We'll talk about a few of these and how you can use them to improve your searches. To enable regular expressions in a search, you must select the Use check box and select Regular Expressions in the Use combo box.

The set expression [ ] is similar to the wildcard expression we described earlier. This expression is used to include a number of different characters in a search. One powerful feature of this expression is that you can specify a range of characters to search for by placing a dash between two characters. For example, to search for any character in the series a through d in a regular expression, you can specify [a-d] in the Find What combo box. The expression [a-d]b searches for any of the following character sets in the Text Editor: ab, bb, cb, and db.

Wherever there's a Find option, you'll have a Don't Find optionin this case, the expression [^ ] weeds out the matches you don't want to see. For example, the expression [^t-v]s returns a match for as, bs, cs, ds, and so on. It skips ts, us, vs, and so forth.

If you want to find something at the beginning of a line, you can place the ^ character at the beginning of your search string. Using this expression alone will match the beginning of each line in a file. The $ character indicates a search at the end of a line. Two other expressions match the beginning and the end of a particular word. The < character matches the beginning of any word in the search string, and the > character matches the end of any word. These expressions must appear in a logical place in your search string. For example, if you want to search for all instances of the word int in a file, you can specify <int as your search string. This returns int as part of a larger word as long as int is at the beginning of the word. Integer and int both match, but mint doesn't match. If you want to take this a step further and search only for the word int, you can just add a closing > expression.

You can create some pretty complex expressions with all the regular expression tools at your disposal. You might want to use these types of expressions to search your code for patterns that match a statement of a particular form. For example, if you want to search for C# MessageBox statements that provide only one string argument to the function, your expression might look something like this:


<MessageBox.Show\(:q|(<.>)\);

That expression is somewhat complex, but it contains elements we've already discussed. The < expression indicates the beginning of a word. The words MessageBox.Show are what we're looking for. The \ expression indicates that the next character is a literal and not a regular expression element. The :q expression is interesting. It's a quoted string expression. It represents the same thing as typing (("[^"]*")|('[^']*')). The | expression that follows :q is an or expression. The string you find in MessageBox.Show might be a string variable, so you can test for any single word by putting <.> in parentheses. The . represents any character, and you use parentheses to contain the or operation. Finally, you check for a closing semicolon.

There's a problem with this expression, though, and it has to do with how the method calls are formatted in the Code Editor. If the programmer of the method call that you're searching for left white space between the end of the parameter and the closing parenthesis, you lose your match. You can adjust for that by adding the :Wh expression followed by the * expression, which will match any or no instance of the preceding expression. So now you're up to <MessageBox.Show\(:q|(<.>):Wh*\);. But what about space before the quoted string, <MessageBox.Show\( Wh*:q|(<.>):Wh*\);? What about before the semicolon? It goes on and on. Regular expressions are excellent tools for searching for text, but you really need to spend some quality time learning to work with them effectively. To that end, we recommend two books that cover regular expressions nicely. The first is Mastering Regular Expressions by Jeffrey E. F. Friedel (O'Reilly, 1997). The second book is Writing Secure Code, Second Edition, by Michael Howard and David LeBlanc (Microsoft Press, 2003). The Friedel book does a good job of explaining regular expressions, and Writing Secure Code gives you some insight into uses for regular expressions that you might not have considered before.

Table 3-8 contains the expressions we've discussed in this chapter. See Visual Studio .NET Help for the complete list. (Click Help in the Find dialog box, and on the help page that opens, you'll find a link to all the regular expressions supported in Visual Studio .NET.)

Table 3-8. Casual Regular Expressions

Expression


Description


[ ]


Find using provided set of characters. You can express a range using the - character.


[^ ]


Find not including the provided set of characters.


< or >


Find the beginning of a word or find the end of a word.


^ or $


Find at the beginning of a line or find at the end of a line


.


Match any single character.



The next character in the search string is a literal character and not a regular expression.


*


Match regardless of character or characters.


:q


Match a quoted string.


:Wh


Match any type of white space .


Searching from the Command Window


Putting this all together, you might find it easier to perform a complex search operation from the Command Window. The Command Window gives you a lot more room for your search strings, especially if you're using wildcards or regular expressions in your searches.

To use the Find command from the Command Window, type Edit.Find followed by the string you want to search the current file for and press Enter. If you want to specify any of the switches associated with this command, type them after the search string you're entering. For example, if you want to use a wildcard in your search, your command might look something like this:


Edit.Find Class# /Wild

The rest of the Find command switches match the options in the Find dialog box that we described earlier. Unless you reset them, these switches stick after each search as if you selected them in the dialog box. So you don't have to specify them every time you perform a search from the command line. To find out what Find options are currently set, enter Edit.Find /options, as shown here:


>Edit.Find /options
/wild /doc /names /sub
>

Table 3-9 lists most of the switches that apply to the Find command in Visual Studio .NET. You'll find that these are similar for Edit.Replace, Edit.FindinFiles, and Edit.ReplaceinFiles. To get the full list, enter help Edit.Find in the Command Window.

Table 3-9. Find Command Switches

Switch


Short Form


Description


/case


/c


Case-sensitive


/doc


/d


Search current document


/hidden


/h


Search hidden code


/markall


/m


Marks each occurrence in the Margin Indicator Bar


/open


/o


Search all open documents


/options


/t


Display the current Find options


/proc


/p


Search current procedure only


/sel


/s


Search the currently selected text only


/up


/u


Search up


/regex


/r


Search with regular expressions


/wild


/l


Search using wildcards


/word


/w


Search for whole word only

Note

The Edit.Find command also contains a /reset switch that is supposed to reset the Find options to a default setting. This switch currently has a bug that requires you to enter a parameter with the switch.


Incremental Searching


Incremental searching is a feature of Visual Studio .NET that's a real timesaver. An incremental search is performed one character at a time, matching each word in the search string from the top of the file. You can start an incremental search by pressing Ctrl+I. You'll see the mouse pointer transform into a down arrow like the one shown in Figure 3-23.


Figure 3-23. Starting an incremental search by pressing Ctrl+I



With the down arrow showing, start typing the term you want to search for. As you type, words that match the letters are matched starting from the top. When you've completed the pattern you want to search for, press Ctrl+I again to move to the next match. You can continue to press Ctrl+I until you reach the end of the document to match every instance of the term you're searching for. If you want to search upward, just press Ctrl+Shift+I. It works in just the same way. You can exit the incremental search by pressing Enter or Esc.


/ 118