Word Wildcards Info

(compiled from many online resources for references)

Turn on display of characters by hitting Ctrl + Shift + 8

To find:

  • Any single character
    • Type ?
    • For example, s?t finds “sat” and “set”.
  • Any string of characters
    • Type *
    • For example, s*d finds “sad” and “started”.
  • The beginning of a word
    • Type <
    • For example, <(inter) finds “interesting” and “intercept”, but not “splintered”.
  • The end of a word
    • Type >
    • For example, (in)> finds “in” and “within”, but not “interesting”.
  • One of the specified characters
    • Type [ ]
    • For example, w[io]n finds “win” and “won”.
  • Any single character in this range
    • Type [-]
    • For example, [r-t]ight finds “right” and “sight”. Ranges must be in ascending order.
  • Any single character except the characters in the range inside the brackets
    • Type [!x-z]
    • For example, t[!a-m]ck finds “tock” and “tuck”, but not “tack” or “tick”.
  • Exactly n occurrences of the previous character or expression
    • Type {n}
    • For example, fe{2}d finds “feed” but not “fed”.
  • At least n occurrences of the previous character or expression
    • Type {n,}
    • For example, fe{1,}d finds “fed” and “feed”.
  • From n to m occurrences of the previous character or expression
    • Type {n,m}
    • For example, 10{1,3} finds “10”, “100”, and “1000”.
  • One or more occurrences of the previous character or expression
    • Type @
    • For example, lo@t finds “lot” and “loot”.

Notes:

  • You can use parentheses to group the wildcard characters and text and to indicate the order of evaluation. For example, type <(pre)*(ed)> to find “presorted” and “prevented”.
  • You can use the \n wildcard to search for an expression and then replace it with the rearranged expression.
    For example, type (Newton) (Christie) in the Find what box and \2 \1 in the Replace with box. Word will find “Newton Christie” and replace it with “Christie Newton”.

Search by using codes

Use codes to find and replace

  1. On the Edit menu, click Find or Replace.
  2. If you don’t see the Special button, click More.
  • Enter a code in the Find What box. Do one of the following:
    • To choose a code from a list, click Special, click a character, and then type any additional text in the Find What box.
    • Type a code directly in the Find What box.
      • For example, type ^p to find a paragraph mark.
  • If you want to replace the item, enter what you want to use as a replacement in the Replace with box.
  • Click Find Next, Replace, or Replace All.
    • To cancel a search in progress, press ESC.

Codes for items you want to find and replace
As specified, some codes work only if the Use wildcards option is on or off.

Codes that work in the Find what or Replace with box:

  • To specify:
    • Paragraph mark ( )
      • Type ^p (doesn’t work in the Find what box when wildcards are on)
    • Tab character (→)
      • Type ^t
    • ANSI or ASCII characters
      • Type ^0nnn, where nnn is the character code
    • Em dash ( — )
      • Type ^+
    • En dash ( – )
      • Type ^=
    • Caret character
      • Type ^^
    • Manual line break (i)
      • Type ^l
    • Column break
      • Type ^n
    • Manual page break
      • Type ^m (also finds or replaces section breaks when wildcards are on)
    • Nonbreaking space (°)
      • Type ^s
    • Nonbreaking hyphen (-)
      • Type ^~
    • Optional hyphen ({)
      • Type ^-
  • Code that works in the Find what box only (when wildcards are on)
    • Graphic
      • Type ^g
  • Codes that work in the Find what box only (when wildcards are off)
    • Any character
      • Type ^?
    • Any digit
      • Type ^#
    • Any letter
      • Type ^$
    • Footnote mark
      • Type ^f
    • Endnote mark
      • Type ^e
    • Field
      • Type ^d
    • Section break
      • Type ^b
    • White space
      • Type ^w (any combination of regular and nonbreaking spaces, and tab characters)
  • Codes that work in the Replace with box only
    • Windows Clipboard contents
      • Type ^c
    • Contents of the Find what box
      • Type ^&

Notes:

  • If you’re searching for fields, you must display field codes. To switch between displaying field codes and field results, click the field, and press SHIFT+F9. To show or hide field codes for all fields in the document, press ALT+F9.
    ——————————————————————————

Part 2:

  • ?= Any 1 character
  • *=Any string of characters
  • @ any number of repeats of the previous character
  • <=the beginning of a word
  • >=the end of a word
  • {n}=n repeats of the previous char
  • {n,}=at least n repeats
  • {n-m}= between n and m repeats
  • [ ] marks a set of characters
  • A – used inside this means an ascending range between the two hyphenated characters
  • A !, only valid at the set’s start, means ‘any character except’.
  • () groups the expressions and indicates the order of evaluation. It is used with the n wildcard to rearrangeexpressions. The result of the 1 st () pair is represented by 1, the next pair 2 and so on.

The easiest way to use a special character as a literal, eg to find a {, use the ASCII code instead. ASCII codes are specified in ANY sort of search with the ^.
= ^92              {=^123              }=^125             ?=^63
*=^42             [=^91                 ]=^93               ^=^94
@=^64            <=^60                >=^62
——————————————————————————

Part 3:

  • When the Use wildcards check box is selected, Word finds only the exact text that you specify. Notice that the Match case and Find whole words only check boxes are unavailable (dimmed) to indicate that these options are automatically turned on. You can’t turn off these options.
  • To search for a character that’s defined as a wildcard, type a backslash (\) before the character. For example, type \? to find a question mark.
  • You can use parentheses to group the wildcard characters and text and to indicate the order of evaluation. For example, type <(pre)*(ed)> to find “presorted” and “prevented”.
  • You can use the \n wildcard to search for an expression and then replace it with the rearranged expression. For example, type (Ashton) (Chris) in the Find what box and \2 \1 in the Replace with box. Word will find Ashton Chris and replace it with Chris Ashton.

To find                                                                              Type       Example
Any single character                                                      ?              s?t finds sat and set.
Any string of characters                                                *              s*d finds sad and started.
The beginning of a word                                               <             <(inter) finds interest and                                                                                                                   intercept, but not splintered.
The end of a word                                                           >          (in)> finds in and within, but                                                                                                               not  interesting.
One of the specified characters                                   [ ]          w[io]n finds win and won.
Any single character in this range                              [-]         [r-t]ight finds right and sight.                                                                                                              Ranges must be in ascending                                                                                                               order.
Any single character except the characters in the
range inside the brackets                                             [!x-z]    t[!a-m]ck finds tock and tuck,                                                                                                               but not tack or tick.
Exactly n occurrences of the previous character
or expression                                                                   {n}         fe{2}d finds feed but not fed.
At least n occurrences of the previous character
or expression                                                                   {n,}        fe{1,}d finds fed and feed.
From n to m occurrences of the previous
character or expression                                                {n,m}     10{1,3} finds 10, 100, and 1000.
One or more occurrences of the previous
character or expression                                                  @          lo@t finds lot and loot.

Use codes to find letters, formatting, fields, or special characters

Use the following codes to find letters, formatting, fields, or special characters. Note that some codes work only if the Use wildcards option is turned on or off.

Codes that work in the Find what box or Replace with box:
To Find                           Type
Paragraph mark ( )       ^p (doesn’t work in Find what box when Use wildcards is turned
on), or ^13
Tab character ( )            ^t or ^9
ASCII character             ^nnn, where nnn is the character code.
ANSI character              ^0nnn, where 0 is zero and nnn is the character code
Em dash ( — )                 ^+
En dash ( – )                    ^=
Caret character              ^^
Manual line break ( )    ^l or ^11
Column break                ^n or ^14
Page or section break   ^12 (when replacing, inserts a page break)
Manual page break       ^m (also finds or replaces section breaks when the Use wildcards                                                     option is turned on)
Nonbreaking space ( )        ^s
Nonbreaking hyphen ( )    ^~
Optional hyphen ( )             ^-

Code that works in the Find what box only (when Use wildcards is turned on)

To find                                                 Type

Picture or graphic (inline only)      ^g

Codes that work in the Find what box only (when Use wildcards is turned off)

To Find                                      Type
Any character                                    ^?
Any digit                                             ^#
Any letter                                           ^$
Unicode character                           ^Unnnn where nnnn is the character code
Picture or graphic (inline only)    ^1
Footnote mark                                  ^f or ^2
Endnote mark                                   ^e
Field                                                    ^d
Opening field brace
(when field codes are visible)       ^19
Closing field brace
(when field codes are visible)       ^21
Comment                                           ^a or ^5
Section break                                    ^b
Em space (Unicode)                          ^u8195
En space (Unicode)                           ^u8194
White space                                       ^w (any combination of regular & nonbreaking spaces,                                                                        and tab characters)

Codes that work in the Replace with box only

To find                                                                     Type
Microsoft Windows Clipboard contents              ^c
Contents of the Find what box                              ^&

Part 4:

The Theory

1. ? and *

  • The two most basic wildcard characters are ? and *. These are essentially similar in use.
  • ? is used to represent a single character and * represents any number of characters. On their own, these have limited use.
  • s?t will find sat, set, sit, sat and any other combination of three characters beginning with “s” and ending with “t”. It will also find that combination of letters within a word, thus it would locate the relevant (highlighted) part of inset etc.
  • s*t will find all the above, but will also find “secret”, “serpent”, “sailing boat” and “sign over document”, etc.

Note: Unlike ?, which always looks for the missing character, * will also find occurrences of the two adjacent characters (here “s” & “t”) with no text between them e.g. “streaker”. It is a blunt instrument and must be used with care, or with other characters to tie it down to only the text you require. There is no limit to the amount of text that the * character might match. It can end up matching all of your text in a multi-megabyte document!

2. @

  • @ is used to find one or more occurrences of the previous character. For example, lo@t will find lot or loot, ful@ will find ful or full etc.

3. < >

  • Used with any of the above (or any other combination of wildcards and characters), you can use the tags < and > to mark the start and end of a word, respectively. Thus, building on the example we used for the “*” character: <s*t> would find “secret” and “serpent” and “sailing boat”, but not “sailing boats” or “sign over documents”. It will also find “’set” in “tea-set” , but not “set” in “toolset”.
  • Again, beware of using “*”, as <s*t> will find any block of text from a word starting with “s” to the end of the next word in the document that ends with “t”, which may not always be what you had in mind.
  • The <> tags can be used in pairs, as above; or individually, as appropriate. For instance, ful@> will find “full” and the appropriate part of “wilful”, but will not find “wilfully”.

4. [ ]

  • Square brackets are always used in pairs and are used to identify specific characters or ranges of characters. For example:
    • [abc] will find any of the letters a, b, or c. [F] will find upper case “F”.
    • [A-Z] will find any upper case letter; [0-9] will find any single number; [13579] will find any odd numbers; [0-9A-z] will find any numbers or letters.
  • You can use any character or series of characters in a range [ ], including the space character. Characters are processed in alphanumeric order – lowest first. If you are uncertain which character is lower than another, look in the Insert + Symbol dialog.

5. \

  • If you wish to search for a character that has a special meaning in wildcard searches – the obvious example being “?” – then you can do so by putting a backslash in front of it:
    • [\?] will not find “\” followed by any character; but will find “?” instead.
  • If you wish to find the backslash itself then you need to precede that with a backslash [\\]. It’s best if you always put the double-backslash into its own range, as in [\\] – sometimes it doesn’t work, otherwise.
  • As previously mentioned, the following is a list of the characters that have a special meaning in wildcard searches ( [ ] { } < > ( ) – @ ? ! * \ ).

6. [!]

  • [!] is very similar to [ ] except in this case it finds any character not listed in the box so [!o] would find every character except “o”. You can use ranges of characters in exactly the same was as with [ ], thus [!A-Z] will find everything except upper case letters.

7. { }

  • Curly brackets are used for counting occurrences of the previous character or expression.
  • {n} This finds exactly the number “n” of occurrences of the previous character (so for example, a{2} will find “aa”).
  • {n,} finds at least the number “n” occurrences; so a{2,} will find “aa” and “aaaa”).
  • {n,m} finds text containing between “n” and “m” occurrences of the previous character or expression; so a{2,3} will find “aa” and “aaa”, but only the first 3 characters in “aaaa” ).

Note: Counting can be used with individual characters or more usefully with sets of characters e.g. [deno]{4} will match done, node, eden); or with bracketed groups: (ts, ){3} will match ts, ts, ts, .

(Unfortunately, there is no wildcard to search for “zero or more occurrences” in Word wildcard searches; [!^13]{0,} does not work).

8. ( )

  • Round brackets have no effect on the search pattern, but are used to divide the pattern into logical sequences where you wish to re-assemble those sequences in a different order during the replace – or to replace only part of that sequence. They must be used in pairs and are addressed by number in the replacement e.g.
    • (John) (Smith) replaced by \2 \1 (note the spaces in the search and replace strings) – will produce Smith John
  • or replaced by \2 alone will give Smith.

Note: The placeholders \1, \2 etc., can also be used in the search string to identify recurring text. e.g. Fred Fred could be written (Fred) \1.

Round brackets are perhaps the most useful aspect of complex wildcard search and replace operations.

9. ^

  • The ^ (“caret”) character is not specific to wildcard searches but it sometimes has to be used slightly differently from normal, when searching for wildcards.
  • In the Find and Replace dialog, if you click in the “Find what” and “Replace with” boxes, and click the “Special” button, you will see a list of supported special characters that you can use; select one and a code will be inserted in the box for you. The ones near the bottom of the list insert special ^ codes, such as ^t for a tab. Once you know what the codes are, you can type them straight in without using the “Special” button.
  • Unfortunately, the range of options available from the “Special” button when you do a wildcard search is more limited than in an ordinary search. Some notable examples, and their workarounds:
    • You may wish to identify a character string by means of a paragraph mark ?. The normal search string for this would be ^p. ^p does not work in wildcard search strings! It must, however, be used in replace strings, but when searching, you must use the substitute code ^13.
    • If you use ^13 in a replace string, invalid characters, that look like paragraph marks but aren’t, will be inserted – so beware! Wildcard searches will also not find footnote/endnote marks – substitute ^2. In normal searches you can use ^u followed by the character number to find a character by code (which is useful for finding non-keyboard characters). That doesn’t work in wildcard searches (and even in the replacement text of normal searches). So you have to either paste the character itself into the search text, or use the Alt-XXXX-trick to type it (the latter doesn’t work in Word 97).
    • There doesn’t seem to be any way to find fields in a wildcard search. Even ^19 (the field code character code) doesn’t work in wildcard searches – this seems to be a bug.
  • For most other special characters and objects, you can use the same codes as in simple searches (^l = manual line break, ^g = graphic, ^^ = caret …).
  • In many cases, characters can be addressed using their character numbers. The obvious example is ^13 for “paragraph”, as mentioned above. Unfortunately, this has not been implemented in a completely predictable or reliable manner, and if you have a need to search using character numbers, ensure that the search works with a test sample before committing to it.

The practice:

Example 1: Transpose first name and surname

There are many occasions when you are presented with blocks of text or numbers etc., where the order of the text is not what you might require in the final document. Swapping the placement of forename and surname as above is one such example – and don’t forget you can add to the replacement, even when using bracketed replacements

For instance, you may wish John Smith to appear as Smith, John.

Or, more likely, you may have a column of names in a table, where you wish to exchange all the surnames with all the forenames.

  • John Smith
  • Brian Jones
  • Alan Williams

You could do them one at a time, but by replacing the names with wildcards, you can do the lot in one pass.

Let’s then break up the names into logical sequences that can only represent the names:

At its simplest, we have here two words – John and Smith. They can be represented by<*>[space]<*> – where [space] represents a single press of the spacebar.

Add the round brackets (<*>)[space](<*>) and replace with \2[space]\1

Run the search on the column of names and all are swapped. Run it again and they are swapped back.

Note: If you get it wrong, remember that Word’s “undo” function (Ctrl+Z) is very powerful and has a long memory!

If some of the names contained middle names and/or initials

If some of the names contained middle names and/or initials, you would first have to convert your table to text (separated by paragraph marks). Select Table + Convert Table to Text.

Or if there is more than one column in your table, paste your “Name” column into a new document, and then convert that column to text.

You could then replace:
(<*) ([! ]@)^13

with:
\2, \1^p

This would convert:

  • John F. Kennedy  to  Kennedy, John F.
  • J. Smith   to  Smith, J.
  • John Smith  to  Smith, John

Finally, convert the text back to a table. (Select Table + Convert Text to Table).

If there was more than one column in your original table, then when converting the text back to a table, be sure to select “Paragraphs”, where it says “Separate text at”. Then, in your original table, delete the old column and paste in the new one.

Example 2: Transposing dates

Another useful example might be the changing of UK format dates to US format dates – or vice versa.

7th August 2001 to August 7, 2001

(For a similar example, see also Transpose dates from mm/dd/yy to yy/mm/dd.)

To give an example of how most of the wildcard characters could be used in one search sequence to find any UK date formatted as above, the following search pattern will do the trick:

[0-9]{1,2}[dhnrst]{2} <[AFJMNSOD]*> [0-9]{4}

Breaking it down:
[0-9] looks for any single digit number, but dates can have two numbers; so to restrict that to two, we use the “count” function {}. We want to find dates with 1 or 2 numbers so:

[0-9]{1,2}

Next bit is the ordinal “th” – Ordinals will be “nd”, “st”, “rd”, or “th” so identify those letters specifically:

[dhnrst]

There will always be two letters, so restrict the count to 2:

[dhnrst]{2}

Next comes the space. You can insert a space with the spacebar [space].

The month always begins with one of the following capital letters – AFJMNSOD. We don’t know how many letters this month has so we can use the blanket “*” to represent the rest. And we are only interested in that word so we will tie it down with <> brackets:

<[AFJMNSOD]*>

There’s another space [space] followed by the year. Years will always have four numbers so:

[0-9]{4}

Finally, add the round brackets to provide a logical break-up of the sequence:

([0-9]{1,2})([dhnrst]{2})[space](<[AFJMNSOD]*>)[space]([0-9]{4})

and replace with:

\3[space]\1,[space]\4

(where [space] represents pressing the spacebar once) to re-order the sequence.

US style manuals specify that ordinals should not be used in dates, but if you did want to keep the ordinals (so converting 7th August 2001 to August 7th, 2001), you could replace

([0-9]{1,2}[dhnrst]{2})[space](<[AFJMNSOD]*>)[space]([0-9]{4})

with:

\2[space]\1,[space]\3

You can use the same logic in reverse to convert August 7th, 2001 to 7th August 2001; or to convert August 7, 2001 to 7 August 2001. Unfortunately you can’t add the ordinals using a wildcard Find & Replace, if they’re not there to begin with – you would need to use a macro if you wanted to do that.

Example 3: Adding or removing the period in salutations

Assume you are parsing addresses and wish to separate the honorific from the name. American usage puts a period at the end (Mr., Mrs., Dr.) while British usage omits the period.

([DM][rs]{1,2})( )

will find Mr Ms Mrs Dr without the period and

\1.\2

will put one in.

or vice versa:

([DM][rs]{1,2}).

will find Mr. Ms. Mrs. Dr. with the period and

\1

will take it out.

Example 4: Duplicate paragraphs (and rows)

(*^13)\1\1 will match any sequence of three identical paragraphs.

If you replace:

(*^13)\1

with

\1

it will delete all consecutive duplicate paragraphs in the document. Repeat until nothing is found, to delete all duplicate paragraphs in the document (as long as you have sorted the text first).

To delete duplicate rows in a table (provided you have no merged cells), you can convert the table to text (Table + Convert to Text, using a tab delimiter); delete the duplicate paragraphs using the above method, then convert the text back to a table.

Example 5: Tags

\<([!\<\>]@\>)*\</\1

will match any well-formed XML element including start-tag and end-tag such as:

<p>some text</p>

or

<customer-name>John Smith</customer-name>

Delete html tags or sgml tags or other bracketed tags (<example>) from a document without affecting other text

Use Word’s Find and Replace dialog, as follows:

  • In the Find What box, enter the following string:
    • \<*\>
  • 2. Leave the Replace With box empty.
  • Place an X in the “Use Wildcards” box.
  • Click Replace All.

Example 6: Formatting
By building up appropriate patterns, you can search for almost any combination of characters.

Of course you can also restrict your searches by specifying some style or formatting, or add formatting for the replacement text. See Finding and replacing non-printing characters (such as paragraph marks), other special characters, and text formatting for more on this.

A nice trick if you want to apply formatting to a part (but not all) of the search text is to put in “tags” in a  first replacement.

In a find/replace, you can only change the formatting of the whole find-text; so you would need to do two find-replaces to get the job done. In the first, you would “tag” the text that has to be formatted; in the second, you format them (and remove the tags).

Find what:

(something)(something else)(another string)

Replace with:

\1$$\2##\3

and then remove the tags and apply the formatting in a second replace:

Find what:

$$(*)##

Replace with:

\1 ((bold))

Imagine, for instance, that you’ve got a text file, and headings are marked up by having 3 empty paragraphs before them and an empty paragraph after.

Find what:

^13{4}([!^13]@^13)^13

Replace with:

^p<H1>\1

Then Find what:

\<H1\>(*)

Replace with:

\1 ((style “Heading 1”))

This will remove the empty paragraphs and format the headings.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s