Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
You can easily navigate within your document using a keyboard and mouse, but if you have many pages to scroll through, it will take quite a while to find specific text in a long document. It will be more time consuming when you want to replace certain characters or words that you have used in your document. The “Find and replace” functionality enables you to find a sequence of characters in a document and replace it with another sequence of characters.
Aspose.Words allows you to find a specific string or regular expression pattern in your document and replace it with an alternative without installing and using additional applications such as Microsoft Word. This will speed up many typing and formatting tasks, potentially saving you hours of work.
This article explains how to apply string replacement and regular expressions with the support of metacharacters.
Aspose.Words provides two ways to apply the find and replace operation by using the following:
In addition, you can use special metacharacters with simple string replacement and regular expressions to specify breaks within the find and replace operation.
Aspose.Words presents the find and replace functionality with the Aspose.Words.Replacing namespace. You can work with many options during the find and replace process using FindReplaceOptions class.
You can use one of the Replace methods to find or replace a particular string and return the number of replacements that were made. In this case, you can specify a string to be replaced, a string that will replace all its occurrences, whether the replacement is case-sensitive, and whether only stand-alone words will be affected.
The following code example shows how to find the string “CustomerName” and replace it with the string “James Bond”:
You can notice the difference between the document before applying simple string replacement:
And after applying simple string replacement:
A regular expression (regex) is a pattern that describes a certain sequence of text. Suppose you want to replace all double occurrences of a word with a single word occurrence. Then you can apply the following regular expression to specify the double-word pattern: ([a-zA-Z]+) \1
.
Use the other Replace method to search and replace particular character combinations by setting the Regex
parameter as the regular expression pattern to find matches.
The following code example shows how to replace strings that match a regular expression pattern with a specified replacement string:
You can notice the difference between the document before applying string replacement with regular expressions:
And after applying string replacement with regular expressions:
You can use metacharacters in the search string or the replacement string if a particular text or phrase is composed of multiple paragraphs, sections, or pages. Some of the metacharacters include &p for a paragraph break, &b for a section break, &m for a page break, and &l for a line break.
The following code example shows how to replace text with paragraph and page break:
You can find and replace text in the header/footer section of a Word document using the HeaderFooter class.
The following code example shows how to replace the text of the header section in your document:
You can notice the difference between the document before applying footer string replacement:
And after applying footer string replacement:
While applying the find and replace operation, you can ignore certain segments of the text. So, certain parts of the text can be excluded from the search, and the find and replace can be applied only to the remaining parts.
Aspose.Words provides many find and replace properties for ignoring text such as IgnoreDeleted, IgnoreFieldCodes, IgnoreFields, IgnoreFootnotes, and IgnoreInserted.
The following code example shows how to ignore text inside delete revisions:
Aspose.Words provides many different properties to find and replace text such as applying specific format with ApplyFont and ApplyParagraphFormats properties, using substitutions in replacement patterns with UseSubstitutions property, and others.
The following code example shows how to highlight a specific word in your document:
Aspose.Words allows you to use IReplacingCallback interface to create and call a custom method during a replace operation. You may have some use cases where you need to customize the find and replace operation such as replacing text specified with a regular expression with HTML tags, so basically you will apply replace with inserting HTML.
If you need to replace a string with an HTML tag, apply the IReplacingCallback interface to customize the find and replace operation so the match starts at the beginning of a run with the match node of your document. Let us provide several examples of using IReplacingCallback.
The following code example shows how to replace text specified with HTML:
The following code example shows how to prepend a line number to each line:
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.