Find / Replace Text

Top  Previous  Next

 VS   VB6   VBA 

The Find / Replace Text feature allows you to find all the occurrences of a string in the source code or text files of your project(s):

ResultsOfFind

There are several improvements in this feature over the Find feature provided by the IDE:

  • The Find Text window (shown below) selects the proper scope of the search automatically, saving you the hassle of selecting it manually. For example, the Find Text window can detect if you are searching occurrences of a parameter and then it sets the scope automatically to the current method.
  • It allows you to exclude occurrences in comments or literals.
  • It allows you to search in a subset of projects or files.
  • It allows you to search multi-line expressions, using the Multiline... button.
  • The results are shown in a multi-tabbed Result window which groups results by file, parent code element, etc. and allows you to store previous searches, to remove results from the list, to re-execute the search, etc.

MZ-Tools offers the following ways to find text in your source code:

  • The MZ-Tools | Find Text... menu.
  • The Find Text... menu entry on the context menu of a code window.
  • The Find Text button on the MZ-Tools - Main toolbar.
  • In Visual Studio 2015 and higher, the keyboard shortcut associated with the MZTools.FindText command.
  • In Visual Studio 2013 and lower, the keyboard shortcut associated with the MZTools8.FindText command.
  • In VB6 / VBA, the keyboard shortcut associated with the Find Text feature.

The Find Text window is the following:

FindText

You can use the following search options:

  • Match case: to consider or ignore the case.
  • Match whole word: to find whole words or to include also substrings.
  • Exclude comments: to ignore occurrences inside comments.
  • Exclude literals: to ignore occurrences inside literals.
  • Use WildCards or Regular Expressions: this setting (that depends on the IDE) allows you to use the characters "?" and "*" as wildcards or regular expressions. When you select one of these options, you can use the Insert > button to insert the wildcard or regular expression. See Wildcards, Regular Expressions (Visual Studio) and Regular Expressions (.NET). For VB/VBA, the .NET regular expressions are supported.

Also, you have the following output options:

  • Use new result tab: this setting allows you to choose if you want to reuse the last result tab or to use a new one.
  • Show code elements: if this setting is not checked, code elements are not shown in the output, only the file where the result was found is shown; if the setting is checked, parent code elements (class, method, etc.) where the result was found are shown too.

Once you have performed the search, the Result Window allows you to replace the found text with another text in some or all of the occurrences, or to delete the whole line where the text has been found. To do this:

  • Show the Replace panel by clicking the Show Text Replace Panel button.
  • Select the option to replace the found text with another text, to replace the whole line where the text was found with a different line that you can compose, or to delete the whole line.
  • Select whether or not to keep open the modified documents.
  • Click the Replace Selected Node button to replace the selected node (with its children) or the Replace All Nodes button to replace all nodes. When a found text of a node is replaced, the node is removed from the list.

Regular expressions allow you to use capturing groups, so that when a subexpression is found, you can reference that subexpression again in the search pattern or in the replace pattern. Capturing groups are identified and backreferenced by numbers (1, 2, ...). To capture a (implicitly) numbered group, surround the desired subexpression with parentheses. You can capture as many groups as you want. Capturing groups can be backreferenced in the search pattern or in the replace pattern, with different syntax, as explained in the following examples:

In a search pattern, numbered capturing groups are backreferenced using the notation \1, \2, etc. For example, the following regular expression searches (useless) assignations of a variable to itself:

 

(\S+) = \1\r

 

where:

  • \S+ matches one or more non-whitespace characters
  • (\S+) matches one or more non-whitespace characters and captures that subexpression as a group (implicitly number 1)
  • \1 backreferences the captured group number 1
  • \r matches the end of line

So, that regular expression would match expressions like this one:

 

var1 = var1

 

In a replace pattern, numbered capturing groups are backreferenced using the notation $1, $2, etc. For example, the following regular expression searches assignations:

 

(\S+) = (\S+)\r

 

where:

  • \S+ matches one or more non-whitespace characters
  • The first (\S+) matches one or more non-whitespace characters and captures that subexpression as a group (implicitly number 1)
  • The second (\S+) matches one or more non-whitespace characters and captures that subexpression as a group (implicitly number 2)
  • \r matches the end of line

So, that regular expression would match expressions like this one:

 

var1 = var2

 

And the following replace pattern would swap the members of the assignation:

 

$2 = $1\r

 

Remarks:

  • In Visual Studio, this feature does not work with Web Forms or HTML pages in Design view, it only works in HTML view.

See Also:

Result Window

Find Text Options

Wildcards

Regular Expressions (Visual Studio)

Regular Expressions (.NET)

Keyboard Shortcuts Options