Finding text within a paragraph or word in MS Word

When using wildcards to search for text between two specific markers, MS Word will find those markers and that text no matter how far apart they are.

If you want to restrict the search to finding that text/markers within a word or a paragraph, you need to be a bit clever about how you search, and think in the negative.

Find within a word

In my case, I wanted to find any place where I had used two carat characters (^) in the same unbroken string of characters, and prepend the first of those characters with a backslash. Turns out the replacement was not so easy either.

FWIW – this was the context I in which I was searching. I’ve highlighted the ^ characters for you.

fabric 1101,1201-2202 show lldp neighbors | egrep "Node|-|apic|^Spine|^Leaf"

Spoiler: the result looked like this – which will find text within any word that has two carat characters (^)

So let’s unpack this. The find pattern above is

([!^ ]@^^)([!^ ]@^^)

Firstly, I’ll remove the parentheses – they come into play for the replacement part.  That leaves us with

[!^ ]@^^[!^ ]@^^

The [! ] sequence says NOT a space character.  The @ symbol says “Any number of … non-space characters

Because the carat character is considered a special character in word search and replace, ^^ is actually searching for a single ^ character

So the whole sequence reads:

Find… any number of non-space characters followed by a ^ followed by any number of non-space characters followed by a ^

That’s the “find” part sorted. But why the parentheses?

The thing is, I want to insert a backslash before the first ^ character, so I have to group by search with () – the first group being all the characters up to, but not including the ^ character. ([! ]@) and the second group being the rest. (^^[! ]@^^)

Which gives us the find pattern as shown above. ([! ]@)(^^[! ]@^^)

The next problem is the replacement part.  For that I use the special MS Word tags \1 and \2 which correspond to the first and second search groups respectively.  But that causes another problem – I actually want to USE a backslash character in my replacement string – and of course it too is a special character. So you’d think, using the logic that if ^^ finds a single ^, then ^\ (or even \) would do in the replacement section to insert a backslash.

But also no. Instead, you need to flash back to when you memorised the set of ASCII characters and remember that the \ character is character number 92

So now I can specify that my replacement string is

Nerd tip: If you want to find repeating patterns in MS Word, you can use the \1 construction in the search for box to, so searching for ( the)\1 would find all occurrences of the word sequence ” the the”

OK. So that will work fine so long as my two carat characters don’t have any spaces between them.  But what about if my source text was a little less concise, with spaces between the target carat characters like:

fabric 1101,1201-2202 show lldp neighbors | egrep "Node|-|apic|^Spine| ^Leaf"

Find within a paragraph

The logic is exactly the same, except this time I need to search for “anything that is NOT a paragraph marker.

MS Word uses the special combination of ^p to mean “paragraph marker” – but it is NOT available when using wildcards!  Instead, I have to go back to that wonderful ASCII set and remember that a CR (carriage return) character is number 13.

So now I can search for “any number of non-CR characters followed by a ^ followed by any number of non-CR characters followed by a ^” using the following sequence.



You don’t need to read why I was doing this.

I’m in the process of converting some documents to asccidoc.

In asciidoc, the ^ characters are used to delimit superscript, so my original line of

fabric 1101,1201-2202 show lldp neighbors | egrep "Node|-|apic|^Spine|^Leaf"

is rendered in asciidoc format as

fabric 1101,1201-2202 show lldp neighbors | egrep "Node|-|apic|Spine|Leaf"

I needed to “escape” the leading ^ character for it to render correctly.

I this site a great help when figuring this out


About RedNectar Chris Welsh

Professional IT Instructor. All things TCP/IP, Cisco or Data Centre
This entry was posted in Microsoft, Microsoft Word, MS Word Tips and tagged , , , . Bookmark the permalink.