Find and Replace Text by Regular Expressions in Word in Java applications
Tags: javaword, findandhighlight, findandreplace, regularexpressions
Published: 2021-09-08 09:15:45

---
title: Find and Replace Text by Regular Expressions in Word in Java applications
published: true
description: find and replace text; find and highlight text in word in java applications
tags: JavaWord, findandhighlight, findandreplace, regularexpressions
//cover_image: https://direct_url_to_image.jpg
---
[Spire.Doc for Java] (!https://www.e-iceblue.com/Download/doc-for-java.html) provides the ability to find the words that match a specific regular expression in a Word document. This article mainly introduce the feature of find and highlight/replace the text by regular expressions from the following two parts.
<ul><li>Find and replace the text by regular expressions in Word document</li>
<li>Find and highlight the text by regular expressions in Word document</li></ul>
Firstly, view the Sample document:
![Sample Word](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmv8j445bam7flw5n0wy.png)
This demo shows how to use the document.replace() method offered by Spire.Doc to find all the text started with # and use Spire.Doc to replace them.

Java:

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

public class WordDemo {
    public static void main(String[] args) throws Exception {
        //Load the  sample document
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        //Define a regular expression for the words that start with #.
        Pattern c = Pattern.compile ("^#(.*?)\\d$");
        //Use the new string “Spire.Doc” to replace all the searched texts.
        document.replace(c,"Spire.Doc");

        //Save the document to file
        document.saveToFile("Result.docx", FileFormat.Docx_2013);
    }
}
```
Output：
![Find and replace text in Word](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv3va60mq8wmaq1y378i.png)

This demo shows how to use the document.findAllPattern() method to search all the contents within【】even they are from the different paragraphs. And then highlight all the searched contents.

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

import java.awt.*;
import java.util.regex.Pattern;

public class WordDemo {
    public static void main(String[] args) throws Exception {

        //Load the sample document
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        Pattern c = Pattern.compile("【[\\s\\S]*】");
        //find all the contents within【】
        TextSelection[] textSelections = document.findAllPattern(c, true); 
       //Highlight the searched contents
        for (TextSelection selection : textSelections) {
            TextRange[] results = selection.getAsRange();
            for (TextRange result : results) {
                result.getCharacterFormat().setHighlightColor(Color.yellow);
            }
        }
        document.saveToFile("Result2.docx", FileFormat.Docx_2013);
    }
}
```
![Find and highlight text in Word](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0d8f7xfveihusyff0iyx.png)