Mastering Notepad++: Exploring Search, Replace Functions with Regex and Plugins

The free and widely-used editor for Windows supports not only basic search and replace functions but also regular expressions. A helpful plugin assists in formulating regex, providing real-time expression validation. However, for many tasks you don’t need regex; built-in functions and plugins often offer simpler solutions.

The raw output of scripts is often extensive and requires some post-processing to eliminate unwanted or superfluous strings. In some cases, this is not enough; a proper transformation is required, for example through sorting or conversion.

If this is not a frequently recurring task, it is easier to use an editor instead of developing a script for this purpose.

Regex Trainer

Searching and replacing constant character strings quickly comes up against its limits. For this reason, all advanced editors also support regular expressions, including Notepad++.

If you open the dialog for Find and Replace with CTRL + H, you can select the desired search mode, including regular expressions.

Find and replace dialog in Notepad

Even though Notepad++ closely resembles the popular Perl regex implementation, formulating expressions often requires support, especially for those who do not work regularly with regex.

For this purpose, the Regex Trainer plugin comes in handy. It can be easily installed via the Plugin Manager and then opened in a separate panel.

In this panel, the tool checks the input of a regular expression in real-time and highlights syntax errors. Furthermore, it includes a Quick Reference where you can look up regex rules.

Regex Trainer checks regular expressions in real-time upon input.

The colour coding of the matches for the regex in the text is particularly useful. It also highlights groups, i.e. the areas in brackets that you can refer to with $1, $2, etc. in the replacement expressions.

Colour highlighting of regex matches and groups

However, the coloring only works if syntax highlighting for specific file types is not active. If necessary, you can deactivate it using the None (Normal Text) option in the Language menu.

The plugin also offers the option to save used expressions along with a description so that they can be reused later.

Integrated functions for text replacement

In numerous circumstances, there is not always a necessity to make the effort of composing a regular expression for the replacement of text. Notepad++ encompasses several functions designed for standard situations.

These incorporate commands for erasing lines, which is tantamount to replacing them with the empty string. These commands can be located in the menu under Edit => Line Operations:

  • Remove Duplicate Lines
  • Remove Consecutive Duplicate Lines
  • Remove Empty Lines
  • Remove Empty Lines (Containing Blank characters)

Functions for deleting lines.

Remove spaces, tabs and line breaks

Notepad++ also offers trim functions that can be used to remove tabs and spaces either at the beginning or end of a line or in both positions. It can also be used to delete line breaks and convert tabs to spaces and vice versa.

Notepad++ offers several integrated trim functions.

Plugin TextFX

TextFX is a longstanding plugin that was not maintained for a while and eventually hasn’t been supported in 64-bit versions of Notepad++. The plugin has been revamped to be compatible with the latest versions of the editor, although it no longer offers as many functions as before.

Install TextFX using the plugin manager.

Like the Regex Trainer, you can install TextFX through the Plugin Manager. Once installed, it creates a dedicated entry in the menu with multiple submenus.

Among its numerous functions, TextFX includes features for converting between different quotation marks and masking them. Additionally, it provides options for URL encoding and removing HTML markup.

The TextFX plugin offers numerous functions for replacing and converting characters.

The TextFX plugin offers numerous functions for replacing and converting characters.

Summary


Posted

in

,

by

Tags: