User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Find, replace, find in files, replace in files, regular expressions
2 posts Page 1 of 1
From time to time, I use Perl regular expressions with backreferences to make otherwise impossibly tedious global replacements. Today, I discovered a quirk in their behavior, at least in the context of the UltraEdit Find and Replace engine.

My original find and replace expressions were as follows.

Find What:
Code: Select all
"C:\\DOCUME~1\\DAG\\MYDOCU~1\\PROGRA~1\\VISUAL~1\\EXE\\Console\\WWLOGGER\\_notes\\(.*?)\.LOG"

Replace With:
Code: Select all
..\Test_Data\\1.LOG

The resulting replacement value is, obviously, not what I expected.

Code: Select all
..Test_Data\1.LOG


It seems that the presence of the backslashes in the text, most of which were part of the replacement literal, confused the Find and Replace engine. This is how I solved it.

Find What:
Code: Select all
"C:\\DOCUME~1\\DAG\\MYDOCU~1\\PROGRA~1\\VISUAL~1\\EXE\\Console\\WWLOGGER\\_notes(\\.*?)\.LOG"

Replace With:
Code: Select all
..\\Test_Data\1.LOG

There are changes in both expressions.

  1. I moved the terminal backslash on the search path string, just to the left of the capturing group, inside the parentheses, making it part of the group.
  2. I doubled all of the backslashes in the replacement string.
This yielded the expected and intended outcome.

Code: Select all
..\Test_Data\WWLoggerRegularDrills.LOG

In retrospect, I realized that doubling the backslashes in the replacement string should have been enough. Reverting to saved, I repeated the test, making only that change. As I anticipated, the amended replacement expression, as follows, also produced the desired result.

Code: Select all
..\\Test_Data\\\1.LOG

Takeaway: When your replacement expression contains both literal backslashes and backreferences to capture groups from a regular expression in the Find What box, you must double the literal backslashes, or they will be interpreted as invalid backreferences, and eaten.

UltraEdit rocks!
The backslash is also the escape character in Unix/Perl regular expression replace strings. It is therefore always necessary to escape a backslash which should be interpreted literally in a replace string with one more backslash for a Unix/Perl regular expression replace. This can be seen also on first replace string ..\Test_Data resulting in ..Test_Data. The \ was interpreted as escape character although T must not be escaped to be interpreted as literal character.

With Perl regular expression already selected in Replace window and clicking on button with .* (in older versions a magnifying glass or a triangle arrow) above (or right of) the replace edit field, a nearly complete list of special codes is listed and clicking on an item in this list results in inserting the code into replace string at current caret position in replace string. The backslash character is also in this list with code \\.
Best regards from Austria
2 posts Page 1 of 1