Java find and replace text on Word document 
Tags: javaapi, wordjava, findandreplace, findandhighlight
Published: 2019-04-29 08:04:19

---
title: Java find and replace text on Word document 
published: True
description: Three different ways to find and replace the text on word document in Java
tags: Java API, Word Java, find and replace, find and highlight
---

On Microsoft Word, we can find special text string and replace them with new text strings easily.  This article will mention various approaches of the finding and replacing feature on the word 

document by using Java API Spire.Doc independently (without using any third-party code) as below.

<ul>

<li>Find and replace text string in Word with new text string </li>
<li>Find and replace Text by Regular Expressions</li>
<li>Find and replace Text with HTML</li>

</ul>
Find and replace text string in Word with new text string. All the searched results will be detected and replaced at one time. 

```
import com.spire.doc.*;
import com.spire.doc.documents.TextSelection;
import java.awt.*;

public class Word {
           public static void main(String[] args) {

               String input = "replaceWithText.docx";
               String output = "out/replaceWithText.docx";
               //load Word document
               Document document = new Document();
               document.loadFromFile(input, FileFormat.Docx);

               //replace the text
               document.replace("Word", "NewWord", false, true);

               //highlight the replaced text

               TextSelection[] textSelections = document.findAllString("NewWord", false, true);

               //set highlight
               for (TextSelection selection : textSelections) {
                   selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
               }

               //save the document
               document.saveToFile(output,FileFormat.Docx);
           }
}
```

Find the words that match a specific regular expression in a Word document and replace the matched words with a new string. The following sample will show you how to replace the regular expression 

for the words that start with”#”.

```
import com.spire.doc.*;
import java.util.regex.Pattern;
import com.spire.doc.documents.TextSelection;
import java.awt.*;


public class Word {
           public static void main(String[] args) {
               String input = "replaceTextByRegex.docx";
               String output = "out/replaceTextByRegex.docx";
               //load Word document
               Document document = new Document();
               document.loadFromFile(input, FileFormat.Docx);

               //create a regex, match the text that starts with "#"
               String regEx="\\#\\w+\\b";
               Pattern pattern= Pattern.compile(regEx);

               //replace the text by regex
               document.replace(pattern,"JavaSpire.Doc");

               //highlight the replaced text

               TextSelection[] textSelections = document.findAllString("JavaSpire.Doc", false, true);

               //set highlight
               for (TextSelection selection : textSelections) {
                   selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
               }


               //save the document
               document.saveToFile(output,FileFormat.Docx);
           }
}
```

Find and replace text in word with HTML.

``` 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;;
import java.io.*;

public class Word{
           public static void main(String[] args) throws Exception{
               String input = "replaceWithHtml.docx";
               String inputHtml = "replaceWithHtml.txt";
               String output = "out/replaceWithHtml.docx";
               //load Word document
               Document document = new Document();
               document.loadFromFile(input, FileFormat.Docx);

               //find a paragraph and replace its content with html
               TextSelection textSelection = document.findString("[#placeholder]", false, true);
               TextRange textrange =textSelection.getAsOneRange();
               Paragraph par= textrange.getOwnerParagraph();
               String htmlText = readTextFromFile(inputHtml);
               par.appendHTML(htmlText);
               par.getChildObjects().removeAt(0);
               //save the document
               document.saveToFile(output,FileFormat.Docx);
           }

    public static String readTextFromFile(String fileName) throws IOException{
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String content = null;
        while ((content = br.readLine()) != null) {
            sb.append(content);
        }
        return sb.toString();
    }
}
```

Conclusion

It is quite easy for us to use Spire.Doc to find and replace the text on the word document with new text strings and HTML in Java applications. Thanks for your reading and wish it helps.