Word .NET > Tutorial > Find and replace options

FindOptions controls how the find and replace methods match text. The search is case sensitive by default. WholeWord matches whole words only, UseRegularExpressions treats the search text as a regular expression and substitutes group references such as $1 in the replacement text, UseWildcards enables the wildcards *, ? and [ ], Scope restricts the search to parts of the document and MaxResults limits the number of matches.

UseWordWildcards switches to the wildcard syntax and the special characters of Microsoft Word's Find dialog: ? and * (any string, shortest first), @, {n,m}, [set] and [!set], < and > for the start and end of a word, ( ) groups referenced as \1 to \9 in the replacement text, \ to escape the next character, and the codes ^p (or ^13), ^t, ^l, ^m, ^-, ^~, ^s, ^g, ^?, ^#, ^$, ^w and ^^. In the replacement text ^& stands for the matched text and ^p, ^t, ^l and ^m for a paragraph mark, a tab, a line break and a page break, which are written as real paragraph, tab and break elements. Independently of the options, a tab, a break, a soft or non-breaking hyphen, a symbol and an inline picture count as one character of the paragraph text, so "ab" never matches "a", tab, "b", a search text may contain \t, \n or \f, and a match covering a picture is found but not replaced.

The paragraph mark is the character \r (^p with Word wildcards). A search text that contains it is matched against the consecutive paragraphs of a body, a table cell, a text box, a header or a note as one text in which every paragraph ends with \r: "a\rb" finds "a" at the end of one paragraph followed by "b" at the start of the next, and "\r\r" finds an empty paragraph. Any other search text is matched against each paragraph on its own, so ^ and $ in a regular expression still mean the start and the end of a paragraph. A replacement that covers a paragraph mark joins the paragraphs, which keep the formatting of the first one, and \r in a replacement text splits a paragraph. Exactly as in Word, the last paragraph mark of a body, a cell or a text box cannot be deleted, a mark that carries a section break is never removed, and a match that starts or ends inside a hyperlink, a field or a content control is not joined.

C# example - Case insensitive, whole words only
C# example - Regular expressions
C# example - Wildcards, scope and maximum number of results
C# example - Word wildcards and special characters
C# example - Paragraph marks
VB example

Need help? Ask our developers: