“Find and Replace” is a huge time saver but sometimes you need something more flexible than replace ‘x’ with ‘y’.
This is where Regular Expressions come in handy.
This article explains how to use wildcards and other advanced features that gives super powers to the humble “Find and Replace” modal.
This article uses a free program called Notepad++. If you don’t have it, you can download it here.
How to Wrap paragraphs with Find/Replace
Consider this example…
Say you have a document formatted like the image above. Imagine you needed to wrap each paragraph in <p> tags. A normal “Find and Replace” can’t do that, but it’s possible with “Regular Expressions”.
- Find and Replace
In Notepad++ press Ctr+H to open the “Find and Replace” window.
- Click “Regular Expression”
Choose Search Mode: “Regular expression” and then check “matches newline”.
- Add <p> tag to beginning of each line
Find what: \n..
Replace with: \n \n<p>
- Notice Added <p> tag
You should see <p> tags at the beginning of each line
- Add Closing <p> tag
Find what: \n \n
Replace with: </p> \n \n
- Notice Closing </p> tags
You should see closing </p> tags at the end of each line.
So what’s happening here? ‘\n‘ looks for a line break. ‘.‘ is a wildcard that matches a single character.
At the beginning we’re saying “find a line-break followed a couple of characters” or in other words “find a new line with content”, then replace it with 2 line-breaks and a “<p>“.
For the closing part, we’re saying “find 2 line breaks in a row” and “replace it with a </p> and 2 line breaks”.
Here’s a list of useful expressions you may want to use in your find and replace escapades…
A digit in the 0-9 range, same as
Not a digit.
A lowercase letter.
NOTE: this will fall back on “a word character” if the “Match case” search option is off.
Not a lower case letter. See note above.
An uppercase letter.
Not an uppercase letter.
A word character, which is a letter, digit or underscore. This appears not to depend on what the Scintilla component considers as word characters. Same as [[:word:]].
Not a word character.
A spacing character: space, EOLs and tabs count.
Not a space.
Horizontal spacing. This only matches space, tab and line feed.
Not horizontal whitespace.
Not vertical whitespace
Only applies to character at the end of a line. (ex. ‘!$’ would replace all explamation points at the end of a line.)
Online applies to character at the beginning of a line (ex. ‘^<p>’ would replace any ‘<p>’ at the beginning of a line.)
Online Tools as an Alternative
Check for differences between to sets of text
The other day, I wanted to check for differences between 2 websites. I had a WordPress theme that I knew I’d made some changes to, but I couldn’t remember exactly what they were.
I found a useful website called https://www.diffchecker.com that let me copy and paste the code for the two sites and highlighted the differences for me.
Changing All Caps to Titlecase
I frequently get text from clients in ALL CAPS, and I may want to change to Title Case or sentence case without having to retype it. TitleCase.com will convert your text to Title Case, UPPERCASE, lowercase, or Sentence case.
It will even do programming specific conversions like camelCase, PascalCase, hyphen-case, or snake_case. Pretty useful!