“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.
Using expressions you could easily replace tabs ()
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.)