Microsoft Word Wildcard and Regular Expression Search and Replace

One often missed aspect of the Find dialog in Microsoft Word is the ability to use wild cards.

In fact, Word not only has the simple wild cards (* and ?) but uses simplified Regular Expression (REGEX) searching.

In this article, I’ve put together a number of examples of advanced finds and replaces that I’ve found useful. I’ll update it from time to time.

Reference

[], groups (), repeats @, {}, anchors < > and exceptions !

^p (to find a paragraph mark) does not work when using wildcard searches. Use ^13 instead. But you cannot use ^13 in the “replace with” field, it doesn’t work.

Use ^2 to find a footnote mark

Word wildcard searches are “lazy” (they stop at the smallest possible match not the largest as most regular expression tools would).

Examples

Don’t forget to turn on “Use Wildcards” to use these examples. It is in the “More” part of the find and replace dialogs.

Remove leading multiple spaces

If you have ever pasted from an email or text document, you may have come across the problem of some indented and manually wrapped text:

Starting Line
   Indented line 1
   Indented line 2

You can find the leading multiple spaces with the code (note that there is a leading space):

 {2,}<

So selecting some text, putting this into the “find what” box and doing a “replace all” with nothing will remove all of the leading spaces in the selection. Actually it will remove everything that has at least 2 spaces followed by the start of a word.

Remove single paragraph marks to join lines

Search for: >^13 and replace with a single space.

Note that you can combine this in a single step with the previous example as (note again leading space):

 {2,}(<*>)^13

Though it should be noted that this can fail. For some reason if the last character on the line is a “/”, Word fails to find a match. Unexplained failures in Word’s find feature is not at all uncommon, it is rather buggy!

To replace manual line breaks instead of a paragraph mark, use ^| instead of ^13. The special characters can be inserted from the “Special” button but do note the warning in “Reference” above and in “What you cannot do!” below regarding some of the marks you cannot find.

What you cannot do!

Recently, I needed to replace a load of text in a very large table (around 100 pages). I knew that the text to replace all appeared at the end of table cells but I couldn’t immediately work out how to search for this. My first thought was to do:

(Done in 1.4a)^13

MS Word Table Cell Example

Which would find the text at the end of a paragraph. This didn’t work so I turned on the visual paragraph marks and saw what I should have already known – the end of a table cell does not have a paragraph mark. As you can see, it has a circular mark with 4 “tails” (sometimes referred to as a “Universal Currency Symbol”). This is an end-of-cell marker. The same symbol appears at the end of the row as well (the end-of-row marker).

Well, it turns out that you cannot search for this! Well, not outside of some VBA anyway. The character is actually an ASCII 07 so you should be able to do:

(Done in 1.4a)^07

And using Replace to add to the text, you would have been able to use:

\1 but there is still more to do

Which would have resulted in “Done in 1.4a but there is still more to do” wherever the original text appeared at the end of a cell.

Sadly though, wildcard searches don’t find end-of-cell marker.

Thankfully, there is another way that works for most searches.

*(Done in 1.4a)

Will find that text at the end of a cell. The leading * consumes all of the leading text in the cell forcing the search to occur at the end. Putting the * in brackets instead of the search text would allow the leading text to be retained in a replace. Putting both in separate brackets would allow the end text to be moved to the start of the cell with a replace string of:

\2 \1 

Links to more information

  • TBC