UltraEdit Find and replace, find in files, and replace in files




UltraEdit and UEStudio give you the ability to perform a find or replace through one or more files. There are multiple configuration options which allow you to target specific file types, project files, directories of files, and more. Using the available tools, you can customize your find or find/replace to quickly make changes to text or code in a directory of files, or to locate a unique string within a single file buried in hundreds of other files, and even find files that do not contain a specific string.

Find and replace, find in files, and replace in files

All of the search features covered here may be accessed under the "Search" menu.

Find and replace, find in files, and replace in files


Find

You can search your open file by selecting the "Find" option from the "Search" menu. Once you select this option, a dialog box will prompt you for your search terms. Here you can also specify other "find" options. To quickly search, type in what you are looking for and press "Find Next."

Find and replace, find in files, and replace in files

The first found string will be highlighted in the file while the Find dialog stays open. You can then step through all occurrences of the string in the file by pressing F3, which will take you to the next instance found. Press CTRL+F3 to return to the previous instance.

You are given several options in the find dialog that you can use to customize your search.

Match Whole Word Only

UltraEdit will not restrict a search to matching whole words only. This option restricts the search to whole words only. Whole words are words separated, on both sides, by a space.

For example, select this option if you want to search for 'a', or 'A' without finding all words that include 'a'.

Match Case

By default your search will not be case sensitive. Selecting "Match Case" will result in UltraEdit searching for only case-sensitive matches.

Regular Expressions

A regular expression search allows you to search for strings that are different but are composed of a similar pattern. For instance, in the below example, our search string of "<.*>" is a special syntax that will match ANY string composed that begins with "<" and ends with ">".

Find and replace, find in files, and replace in files

This search is done using Perl-style regular expressions, which may be configured in Advanced : Configuration : Search : Regular Expression Engine, or by clicking on the Advanced button (on the find dialog) and selecting the particular engine.

The "." in our search string tells UltraEdit to find any character that is not a new line, and the "*" character qualifies that by telling UltraEdit to search for this and match it 0 or more times. Therefore, all of the following strings would be matched:

However, the following string would not be matched since it contains a line break (new line). While the "." character will match any character, it will not match a line break.

You can learn more about regular expressions at our Perl regular expressions power tip.

Find Where

Current File

If you select this option, UltraEdit/UEStudio will perform the search in the current file.

Selected Text

If you select this option, UltraEdit/UEStudio will ONLY search in the currently selected text.

Find Next through all opened files

If you select this option, repeating the Find Next command (F3) will continue to cycle through all open files after finishing the search through the active file.

Beyond the optional items available in the top-level of the Find dialog, there are also Advanced configuration items available. To view the Advanced options, click on the Advanced button.

Using Find, Replace, Find In Files, and Replace in Files

Regular Expression Engine

UltraEdit/UEStudio supports 3 styles of regular expressions syntax - UltraEdit style, Unix Style, and Perl Compatible Expressions. Additional information about the specific syntax is available in the Help under the "Regular Expression Engine" section.

Highlight All Items Found

This option will result in all items matching the search string to be highlighted, or shown as selected text, when the Find Next button is pressed.

List Lines Containing String

Selecting this option will cause UltraEdit to search for all occurrences of the string in the active document and list them in a dialog box. When the list lines containing string dialog opens, you can double-click on a result line and UltraEdit will position to that line in the file. You may also select the result line and click on Goto.


Count All (Count Occurrences)

This button will count the number of occurrences of your search string in the active file.

Find and replace, find in files, and replace in files


Recent Search History

If you would like to view the recent search history, you can select the entire text (Select All - CTRL + A) of the Find dialog and press the up/down arrow to cycle through the history. Additionally, you can click the clock icon to select an item from your search history.

Using Find, Replace, Find In Files, and Replace in Files

UltraEdit/UEStudio also offers a convenient toolbar item that allows you to scroll through a drop-down box of your recent searches. This is especially useful if you need to access a recent search quickly. You can select the search item you need, then press F3 to find the next instance of this search, all without invoking the Find dialog.

Using Find, Replace, Find In Files, and Replace in Files


Search Favorites

The star icon on the Find/Replace dialog allows you to save a list of your favorite searches. This can be especially useful for complex regular expression patterns.

To use an item stored in the favorites list, simply click the Star icon and navigate to Favorites, and select the item.

Using Find, Replace, Find In Files, and Replace in Files


Replace

The "Replace" option under the "Search" menu will invoke the Replace dialog. This allows you to search for a phrase or string and then replace it with a different phrase or string.

Using Find, Replace, Find In Files, and Replace in Files

There are several options for configuring your search in the Replace dialog. For our purposes, we will do a simple search and replace, replacing all occurrences of "1,000,000" with "2,000,000" as shown below.

Using Find, Replace, Find In Files, and Replace in Files

You can select "Replace All," and all instances of your string will be replaced immediately. However, we would like to see our matches before we replace them. To do this, we will press the "Start" button instead. This will find the first occurrence of our find string and highlight it, then bring up a smaller dialog with some replace options.

Using Find, Replace, Find In Files, and Replace in Files

After pressing the "Replace" button, our matched string is replaced. You can press "Replace All" to go ahead with the replace on all occurrences in the file immediately. Pressing the "Find Next" button will take you to the next occurrence of the string without replacing the current one. Pressing "Last" will take you to the last found string, and pressing "Close" will close the Replace dialog altogether; essentially canceling the replace.

As you can see below, our text in the document has been replaced with our replace string.

Using Find, Replace, Find In Files, and Replace in Files


Further Replace Options

There are some additional options and configurations unique to the Replace dialog when you first invoke a replace. The options for "Match Whole Word Only", "Match Case", "Regular Expressions", and "Replace Where" work the same as in the Find dialog (see above).

To see the additional (Advanced) options, click on the Advanced button.

Using Find, Replace, Find In Files, and Replace in Files

Preserve Case

The option to preserve cases causes will match the case of the replace string with the case of the found string. The rules for this are as follows:

  1. If the word found is all lowercase the replacement word will be all lower case
  2. If the word found is all UPPERCASE the replacement word will be all UPPERCASE
  3. If the word found is MixedCase the replacement word will MATCH the casing of the replacement word.

For example, if this option is selected and you search for "The" and replace it with "Then," the following replaces would occur:

Replace All is from Top of File

If this option is selected the specified search will be executed from the top of the active file rather than the current cursor position. This setting is important because a "Replace All" could (potentially) skip a find/replace that is above the cursor position.

Close this dialog after replace

If this option is selected the dialog will close after the command has been executed.


Find in Files

The "Find in Files/Replace in Files" options are accessible under the "Search" menu. These powerful search options allow you to search for strings or text within multiple files contained in a directory.

Using Find, Replace, Find In Files, and Replace in Files

Selecting the Find in Files option will bring up a Find in Files dialog box. This box allows you to configure the parameters of the search.

Using Find, Replace, Find In Files, and Replace in Files

For our example, we would like to find the string "1,000,000" in all of our HTML files in our "htmlBackup" directory.

In Files/Types
Setting the "In Files/Types" to *.html tells UltraEdit to search ONLY HTML files. If you would like to search in multiple file types, you may list additional file types separated by semicolons (ie. *.html; *.txt; *.php).

Directory
You can click the "Browse" button to browse through your computer for the directory you'd like to search. If you wish to search sub directories, below the primary directory, make sure to select the "Search Sub Directories" option.

Once we press the find button, our search begins. On larger directories with many files, the search may take several minutes. By default, the results of our search will be written to an output window, as shown below.

As you can see from the below screenshot, every occurrence found is listed in the output window. Scroll down to see more occurrences. You may double-click the file path to be taken to the line in the file where the search string was found.

Using Find, Replace, Find In Files, and Replace in Files


Further Find in Files Options

The following options are unique to the "Find/Replace in Files" dialogs.

Unicode Search

Selecting this instructs UltraEdit to perform a Unicode search. The string you enter will be entered as multi-byte and converted to Unicode. All files searched will be searched for using the Unicode string.

Search In (Files Listed/Open Files)

Allows you to specify whether the search is conducted in the directory you've specified (Files Listed) or the files that are currently open in the editor.

You will need to click the Advanced button to use some of the options below.

Search Subdirectories

This instructs UltraEdit to search all subdirectories of the specified directory. By default UltraEdit will only search files within the top-level directory you specify.

Results to Edit Window

This option instructs UltraEdit to create an edit window (new file) for the results of the find. This allows you to edit, save, and print the results. As noted above, UltraEdit by default will capture the output in a list box window (Output Window) that allows you to double click on the result and have the file opened automatically.

Ignore Hidden Sub Directories

If this option is selected, the operation will ignore sub directories that are hidden.


Replace In Files

The "Replace in Files" option, located under "Find in Files" in the "Search" menu, is probably one of the most powerful Find/Replace functions in UltraEdit. This allows you to edit massive amounts of files with just one click. Like the other search/replace functions, selecting this option will invoke a "Replace in Files" dialog where you can specify the parameters of your search and replace.

It is important for you to understand the replace in files command before you issue it because all changes are permanent and cannot be "undone". We therefore suggest doing a test before executing the command on the actual data, or making a backup of the actual data before running the replace in files.

In our example below, we have specified that files in our "htmlBackup" directory will be searched for "idmProducts.", and all matches will be replaced with "products.". We have selected the option to "List Changed Files," because we would like to know exactly where UltraEdit found a match and replaced it.

Once you've set these parameters to meet your specifications, you can press the "Replace All" button to begin the Replace in Files. As with the "Find in Files" function, depending on the amount of data, a "Replace in Files" over many files can take several minutes to complete.

Using Find, Replace, Find In Files, and Replace in Files

When the replace is complete the results summary is written to the output box. The summary tells you exactly where the replace occurred and how many times it occurred in each file.

According to our results summary (shown below), our string was replaced 10 times in both "products.coffee" and "products.js". The last line is an overall summary that tells us the string was replaced 20 times in 2 different files.

Using Find, Replace, Find In Files, and Replace in Files

The additional options in the "Replace in Files" dialog, that were mentioned previously for the Find in Files description, perform the same function (as described above) in the Replace in Files.




Getting started with Perl regex in UltraEdit and UEStudio

As one of the most powerful components of UltraEdit's/UEStudio's find/replace functionality, Perl regular expressions give you the power to reformat large amounts of nonuniform data in a single replace, saving you minutes or even hours of manual text editing!

To search for your string go to Search -> Find (or CTRL F). To perform a Perl regular expression search, check the "Regular Expressions" option and ensure the regular expression engine is set to "Perl" in the advanced options of the Find dialog. With Perl regex enabled, type your expression and hit Next. It's that easy!

Example of Perl regular expression Find in UltraEdit/UEStudio

In the example above, we used <td align="right".* to find all <td> elements with a property of align="right". Using Highlight All, you can see that the Perl regex matched all of these elements, even though they contain different data after the align="right" property.

Perl regular expressions use a very simple and understandable syntax. Let's take a look at this syntax below.

Note: Also be sure to read our advanced Perl regular expressions tutorial.


Perl regex syntax quick reference

. Any character, except for new line
^ Start of line position (does not actually match any characters)
$ End of line position (does not actually match any characters)
* Matches previous character 0 or more times
+ Matches previous character 1 or more times
? Matches previous character 0 or 1 time
a{n} Matches "a" repeated exactly n times
a{n,} Matches "a" repeated n or more times
a{n,m} Matches "a" between n and m times
[a-d0-3] Matches a character in the set. This example matches a, b, c, d, 0, 1, 2, or 3
[^a-d0-3]   Matches any character NOT in the set. This example will match anything except for a, b, c, d, 0, 1, 2, and 3 (even new lines)
(foo) Matches "foo" and allows it to be backreferenced in Find and/or Replace string via \1
(?:foo) Matches "foo" but does not store it in memory for backreferencing. This technique can be useful for grouping purposes
| "Or" operator. For example, "foo|bar" will match either "foo" OR "bar"

Use "\" to escape the special meaning of special characters. For example, "\*" will match a literal "*".


Perl regex built-in character classes and positions

\w Any word character
\W Any character that is NOT a word character
\d Any digit character
\D Any character that is NOT a digit character
\s Any whitespace character (including new line characters)
\S Any character that is NOT a whitespace character
\< Matches start of word position (does not actually match any characters)
\> Matches end of word position (does not actually match any characters)
\b Matches beginning or end of word position (does not actually match any characters)
\t Tab
\r Carriage return (CR)
\n Line feed (LF)
\xHH Character with hex value HH

Perl regex output modifiers

Unless otherwise specified, these modifiers can be used in Replace With strings only.

\u Outputs uppercase version of the next character
\U...\E Outputs uppercase version of all characters until \E
\l Outputs lowercase version of next character
\L...\E Outputs lowercase version of all characters until \E
\D Where "D" is a digit, this outputs data matched by a "()" group in Find string. "\1" outputs the first group, "\2" outputs the second group, etc. (It is also possible to use a backreference in the Find string)
$& Outputs all text that matched the entire Find regex
$` Outputs the text between the end of the last match found (or the start of the text if no previous match was found), and the start of the current match
$' Outputs all text following the end of the current match

Perl regex techniques and samples

.* Match zero or more of any characters (except new line). Note: Perl regex are greedy, meaning they will match as much data as possible. For more information, see the non-greedy regex tutorial.
.+ Match one or more of any character (except new line)
[\r\n]+ Match new line character(s) regardless of terminator format
<a href=(".+?")>(.+?)</a> Match hyperlinked text and preserve both the URL and link text for backreferencing
^(.+)$[\r\n]+\1$ Match two duplicate consecutive lines
^.*[ \t]+$ Match lines ending in one ore more spaces or tabs

Now that you are familiar with regular expressions, dig a little deeper in our advanced Perl regex tutorial.


Perl Regular Expressions in UltraEdit: Digging Deeper

In our previous Perl regex power tip, we covered some the fundamentals of Perl regex Find and Replace in UltraEdit. If you're looking to expand your knowledge and harness the power of this popular and robust regular expression engine, take a deeper dive in this power tip.


Make dot "." span multiple lines

If you want your wildcard "." to span multiple lines and not stop at line breaks, then all you need to do is enable "single-line mode" by adding "(?s)" to the beginning of your regex string. For example, the following:

(?s)perl.*regex

...will match the highlighted portion of the following text:

In our previous Perl regex power tip, we covered some the fundamentals
of Perl regex Find and Replace in UltraEdit. If you're looking to
expand your knowledge and harness the power of this popular and robust
regular expression engine, take a deeper dive in this power tip.

If you want your wildcard "." to span multiple lines and not stop at
line breaks, then all you need to do is enable "single-line mode" by
adding "(?s)" to the beginning of your regex
string.


Using lookbehinds and lookaheads

Perl regular expressions include powerful features called "lookbehinds" and "lookaheads", which allow you to check the data in front of and behind a matched string of text, respectively, without including that data as part of your match. The syntax for lookbehinds and lookaheads is as follows:

(?<=a)b Positive lookbehind*; matches "b" when it is preceded by "a" ("a" is not included in the match)
(?<!a)b Negative lookbehind*; matches "b" when it is not preceded by "a"
b(?=a) Positive lookahead; matches "b" when it is followed by "a" ("a" is not included in the match)
b(?!a) Negative lookahead; matches "b" when it is not followed by "a"

* Important note: Because regular expressions do not go backwards for pattern-matching, lookbehinds must be fixed-width, meaning that you must specify the exact length of what to look behind for. For example, the following would be a valid lookbehind:

(?<=foo|bar)hello    (this is ok)

...because both "foo" and "bar" are exactly 3 bytes in length, so the Perl regex engine knows to check the 3 bytes preceding the matched string. However, the following would not be a valid lookbehind:

(?<=f.*)bar    (this is invalid)

...because of the asterisk operator, which represents any number of the preceding character. The regex engine does not know how far back in the data to traverse to begin the lookbehind check. For this reason, it is not possible to use most wildcards and alternations when using lookbehinds.


Find all lines containing two different strings, regardless of order

To find all lines containing two strings regardless of which comes first, for example "cat" and "dog", use the following expression:

^(?=.*cat)(?=.*dog).*$

In the above expression, the positive lookheads (?=.*cat) and (?=.*dog) are essentially telling the regexp engine to match the start of a line (via ^) and "look ahead" to ensure cat and dog exist somewhere before the end of the line. Then, .*$ matches everything up to the end of the line.


Find all lines containing one string, but not another

To find all lines containing one string, for example "cat", but NOT another, for example "dog", use the following expression:

^(?!.*dog).*cat.*$

In the above expression, the negative lookhead (?!.*dog) ensures that no line containing dog will be matched. Then the following .*cat.*$ will ensure that only full lines containing cat somewhere within are matched.


Find a regex in a certain column

You can search for a particular pattern at a specific column position by using the following syntax:

(?<=.{39})f\w+bar

The above example will search for words at column 40 that begin with "f" and end with "bar"


Find characters based on their hex values

Search for a character by its hex value, or even a range of hex values, by using syntax similar to the following which demonstrates a search for non-printable ASCII characters:

[\x00-\x08\x0B\x0C\x0E\x0F\x10-\x1F\x7F]

This technique is used in the "Zap Gremlins" script on our user-submitted script downloads page.


Find Unicode characters via code point

You can also use similar syntax to search for Unicode characters by their code point. For example, the following will search for the Unicode Greek capital Delta Δ (Unicode code point: 0394):

\x{0394}

You can see all Unicode characters and their associated code points here.