Search and Replace

Wordize Replacement for .NET enables search and replace, supports regex, and works with document conversion modules for automated text modifications.

Wordize provides powerful search and replace functionality, allowing developers to find specific text or patterns in documents and replace them programmatically. This feature is useful for updating templates, correcting errors, and automating content modifications across multiple documents.

With Wordize Replacement for .NET, you can:

  1. Find and replace plain text
  2. Use regular expressions for advanced search operations

Search and Replace Simple Text

The simplest way to replace text in a document is by providing a search string and a replacement string. This method is useful for making direct word or phrase substitutions. Use one of the Replace methods to perform a search and replace on a string.

The following code example shows how to perform a simple search and replace of a string in a document using the Replace(string, string, string, string) method:

string doc = MyDir + "Document.docx";
string pattern = "Wordize";
string replacement = "Wordize Pro";

Replacer.Replace(doc, ArtifactsDir + "Document.1.docx", pattern, replacement);

The following code example shows how to perform the same simple search and replace, but changing the output format using the Replace(string, string, SaveFormat, string, string) method:

string doc = MyDir + "Document.doc";
string pattern = "Wordize";
string replacement = "Wordize Pro";

Replacer.Replace(doc, ArtifactsDir + "Document.2.docx", SaveFormat.Docx, pattern, replacement);

The following code example shows how to perform search and replace using documents from the stream with the Replace(Stream, Stream, SaveFormat, string, string) method:

string pattern = "Wordize";
string replacement = "Wordize Pro";

using (FileStream streamIn = new FileStream(MyDir + "Document.doc", FileMode.Open, FileAccess.Read))
{
    using (FileStream streamOut = new FileStream(ArtifactsDir + "Document.3.docx", FileMode.Create, FileAccess.ReadWrite))
        Replacer.Replace(streamIn, streamOut, SaveFormat.Docx, pattern, replacement);
}

Search and Replace Using Regular Expressions

Wordize provides a powerful way to search and replace documents using regular expressions (Regex). This is useful when working with dynamic text such as dates, numbers, placeholders, or recurring phrases.

Using Wordize Replacer for .NET and other options of the Replace method, developers can define complex search patterns and replace them with the desired text.

The following code example shows how to perform a search and replace through a regular expression using the Replace(string, string, Regex, string) method:

string doc = MyDir + "Document.docx";
Regex pattern = new Regex("gr(a|e)y");
string replacement = "lavander";

Replacer.Replace(doc, ArtifactsDir + "Document.4.docx", pattern, replacement);

The following code example shows how to perform a search and replace through the same regular expression, but changing the output format using the Replace(string, string, SaveFormat, Regex, string) method:

string doc = MyDir + "Document.doc";
Regex pattern = new Regex("gr(a|e)y");
string replacement = "lavander";

Replacer.Replace(doc, ArtifactsDir + "Document.5.docx", SaveFormat.Docx, pattern, replacement);

The following code example shows how to perform search and replace using documents from the stream with the Replace(Stream, Stream, SaveFormat, Regex, string) method:

Regex pattern = new Regex("gr(a|e)y");
string replacement = "lavender";

using (FileStream streamIn = new FileStream(MyDir + "Document.docx", FileMode.Open, FileAccess.Read))
{
    using (FileStream streamOut = new FileStream(ArtifactsDir + "Document.6.docx", FileMode.Create, FileAccess.ReadWrite))
        Replacer.Replace(streamIn, streamOut, SaveFormat.Docx, pattern, replacement);
}

Customize Search and Replace

You can work with many options during the search and replace process using FindReplaceOptions:

  • ReplacementFormat
  • MatchCase
  • indWholeWordsOnly
  • IgnoreDeleted
  • IgnoreInserted
  • IgnoreFields
  • IgnoreFieldCodes
  • IgnoreFootnotes
  • UseSubstitutions
  • IgnoreStructuredDocumentTags
  • IgnoreShapes

To work with FindReplaceOptions you can use the following options of the Replace method:

  • Replace(string, string, SaveFormat, string, string, FindReplaceOptions)
  • Replace(Stream, Stream, SaveFormat, String, String, FindReplaceOptions)
  • Replace(string, string, SaveFormat, Regex, string, FindReplaceOptions)
  • Replace(Stream, Stream, SaveFormat, Regex, string, FindReplaceOptions)

The following code example shows how to specify ReplacementFormat to HTML:

string doc = MyDir + "Document.doc";
string pattern = "run";
string replacement = "<b>Text </b><i>with </i><u>formatting</u>";

FindReplaceOptions options = new FindReplaceOptions() { ReplacementFormat = ReplacementFormat.Html };
Replacer.Replace(doc, ArtifactsDir + "Document.7.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to specify ReplacementFormat to Markdown:

string doc = MyDir + "Document.doc";
string pattern = "run";
string replacement = "**Text** *with* `formatting`";

FindReplaceOptions options = new FindReplaceOptions() { ReplacementFormat = ReplacementFormat.Markdown };
Replacer.Replace(doc, ArtifactsDir + "Document.8.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to replace text only if the found pattern is a separate word:

string doc = MyDir + "Document.doc";
string pattern = "Wordize";
string replacement = "Wordize Pro";

FindReplaceOptions options = new FindReplaceOptions { FindWholeWordsOnly = true };

Replacer.Replace(doc, ArtifactsDir + "Document.9.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to ignore text inside fields when replacing:

string doc = MyDir + "Document.doc";
string pattern = "Wordize";
string replacement = "Wordize Pro";

FindReplaceOptions options = new FindReplaceOptions { IgnoreFields = true };

Replacer.Replace(doc, ArtifactsDir + "Document.10.docx", SaveFormat.Docx, pattern, replacement, options);