It is probably safe to assume that anyone who uses a computer will occasionally need to manipulate words within a text editor.
Fortunately, most text editors provide a wonderful tool called “find and replace”, which can often assist in these tasks. Unfortunately, when “find and replace” fails to help, we are often forced to resort to manual labour.
| Problem | Manual Solution |
Have: “” ‘’, need: "" '' | Find and replace, four times |
| List contains too much text | Trim each list item individually |
| Convert words to title case | Capitalize each word individually |
After making such tedious and repetitive corrections time after time, you may eventually wish to possess some way to communicate your intentions to your word processor; to make it do all of that hard work.
Several years ago, I found myself in desperate need of some sort of “bulk find and replace” utility program to solve a particular problem I had.
| Problem |
| Repeatedly replace a long list of characters with different characters |
Back then, as a nonprogrammer, I did what many others might do: search for some miracle app that could help me. Although “bulk find and replace” apps do exist, I was unable to find any (free) one that could do exactly what I required.
However, I discovered a better solution: regular expressions. Since then, I have never been forced to make repetitive edits to text ever again.
Within this blog post, you will be briefly introduced to regular expressions and learn how to use them to painlessly solve problems similar to those listed above through the use of a specific text editing software.
Regular Expressions
What is a ‘regular expression’?
For the purposes of this blog post, a regular expression is piece of code that we will use to add pattern-matching logic to a text editor’s find and replace functionalities.
For a complete definition of regular expressions, as well as regular expression documentation, you are recommended to visit regular-expressions.info, which will be referenced throughout this blog post.
How can I learn regular expressions?
The remainder of this blog post is intended to act as an overview into the use of regular expressions with a specific text editor. You may also choose to treat this blog post as a brief tutorial exercise, and follow along with the tutorial steps provided, or utilize the links at the end of the article.
If you enjoy learning through activity, you may also enjoy the interactive regular expressions tutorial at RegexOne.com.
If you prefer to learn through study, you may wish to reference the Regular Expressions Quick Start Guide.
Notepad++ RegEx Demo
Notepad++ is a free plaintext editor for Windows operation systems. It supports the use of regular expressions to enhance its find and replace operations. From this point onward, you will see Notepad++ being used to demonstrate the use of regular expressions.
Note: multiple implementations of regular expressions engines exist. Notepad++ uses “Perl-compatible regular expressions” (PCRE). To learn more about PCRE, you may wish to reference Notepad++’s own regular expression tutorial.
Setting Up Notepad++
Once you have downloaded Notepad++, launch it to open a new text document and copy in any text that you would like to reformat. For the purposes of this tutorial, we will be reformatting the following sample text:
“DO”: ‘a deer, a female deer’
Adapted from “Do-Re-Mi” from The Sound of Music
“RE”: ‘a drop of golden sun’
“MI”: ‘a name I call myself’
“FA”: ‘a long, long way to run’
“SO”: ‘a needle pulling thread’
“LA”: ‘a note to follow Sew’
“TE”: ‘a drink with jam and bread’
Once you have your text open in Notepad++, open the program’s ‘Replace’ window, which can be accessed through the ‘Search’ menu on the program’s menu bar, or through the keyboard shortcut Ctrl+H:

In order to enable the use of regular expressions, here, click on the ‘Regular expression’ radio button, found in the lower-lefthand corner of the Replace window. After doing so, you are now ready to add regular expressions into the “Find what” and “Replace with” fields. Let’s try this out, now.
Simple Matching and Deletion
Without regular expressions, it is possible to delete all instances of a simple text string using a find-and-replace functionality. Here’s how to do so:
- Enter the text string into the ‘Find’ field.
- Ensure that the ‘Replace’ field is empty
- Press the ‘Replace All’ button
Using regular expressions, we can perform smarter, more-targeted deletions using the same principle. To demonstrate, let’s delete the quotation marks from our Do-Re-Mi list opened in Notepad++.
To do so, we first add an instance of each quotation mark character into the ‘Find’ field, and then enclose those characters in square brackets (denoting a character set): [“”‘’]
Notepad++ will match any single character enclosed in square brackets. To delete those matched characters, simply follow steps 2 and 3, above.
After doing so, our list should now look like this:
DO: a deer, a female deer
RE: a drop of golden sun
MI: a name I call myself
FA: a long, long way to run
SO: a needle pulling thread
LA: a note to follow Sew
TE: a drink with jam and bread
Let’s delete the descriptions of each list item. To to do, we’ll tell Notepad++ to match the colon on each line as well all characters following it. We can do so using the following regular expression: :.+
In a regular expression, a dot (.) is a special, reserved character (metacharacter) that acts like a wildcard; it matches (almost) any single character. The plus sign is a operator that causes its operand to repeat. For example, .+ matches one or more characters, which means that :.+ matches a colon followed by one or more characters.
Capture Groups and Replacement
After deleting our list items, we’re left only the names of the musical tones, in all-caps, and still listed on separate lines:
DO
RE
MI
FA
SO
LA
TE
Let’s quickly reformat our text into a dash-separated list. We can actually do so very easily without the use of regular expressions:
- ‘Find what’:
\r\n(Windows-specific newline escape sequence) - ‘Replace with’:
- - Replace All
We are left with: DO-RE-MI-FA-SO-LA-TE
Our last task will be to convert the second letter of each word to lowercase. In order to do so, we will use two regular expressions. The first, (\u)\b, will be entered into the ‘Find what’ field. The second, \L\1\E, will be entered into the ‘Replace with’ field.
In the first expression, the parentheses denote a capture group, which is referenced by the backreference, \1, within the second expression. The first expression matches every uppercase letter (\u) that is followed by a word boundary (\b).
In the second expression, the backreference is enclosed by case conversion escapes, which converts it to lowercase.
After running the replacement, we have achieved our final result: Do-Re-Mi-Fa-So-La-Te

Next steps
If you are interested in learning more about regular expressions, you may wish to look into any of the following resources:
- regular-expressions.info (referenced throughout this blog post)
- RegexOne.com (regular expression interactive tutorial)
- Notepad++’s regular expression documentation