Highlight Exact match text not Contain match

I want’s to highlight Hello but it’s highlight Hello but its also highlight hello in hello1234.
i need where it will match exact that text only highlight not the contain of match text.

@rabin.samanta

Thanks for your inquiry. It would be great if you please share working source code to reproduce this issue on our end. We will investigate the issue on our end and provide you more information.

@mannanfazil

package com.ver.documents.service.helper;

import java.awt.Color;
import java.util.ArrayList;

import com.aspose.words.IReplacingCallback;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplacingArgs;
import com.aspose.words.Run;

class ReplaceEvaluatorFindAndHighlight implements IReplacingCallback {
	/**
	 * This method is called by the Aspose.Words find and replace engine for each
	 * match. This method highlights the match string, even if it spans multiple
	 * runs.
	 */
	
	Color color = null;

	public void list(Color colors) {
		color = colors;
	}

	public int replacing(ReplacingArgs e) throws Exception {
		// This is a Run node that contains either the beginning or the complete match.
		Node currentNode = e.getMatchNode();
		// The first (and may be the only) run can contain text before the match,
		// in this case it is necessary to split the run.
		 if (e.getMatchOffset() > 0)
		 currentNode = splitRun((Run) currentNode, e.getMatchOffset());
		// This array is used to store all nodes of the match for further highlighting.
		ArrayList runs = new ArrayList();

		// Find all runs that contain parts of the match string.
		int remainingLength = e.getMatch().group().length();
		while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
			runs.add(currentNode);
			remainingLength = remainingLength - currentNode.getText().length();

			// Select the next Run node.
			// Have to loop because there could be other nodes such as BookmarkStart etc.
			do {
				currentNode = currentNode.getNextSibling();
			} while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
		}

		// Split the last run that contains the match if there is any text left.
		 if ((currentNode != null) && (remainingLength > 0)) {
		 splitRun((Run) currentNode, remainingLength);
		 runs.add(currentNode);
		 }

		// Now highlight all runs in the sequence.
		for (Run run : (Iterable<Run>) runs) {
			run.getFont().setHighlightColor(color);
		}

		// Signal to the replace engine to do nothing because we have already done all
		// what we wanted.
		return ReplaceAction.SKIP;
	}

	/**
	 * Splits text of the specified run into two runs. Inserts the new run just
	 * after the specified run.
	 */
	private static Run splitRun(Run run, int position) throws Exception {
		Run afterRun = (Run) run.deepClone(true);
		afterRun.setText(run.getText().substring(position));
		run.setText(run.getText().substring((0), (0) + (position)));
		run.getParentNode().insertAfter(afterRun, run);
		return afterRun;
	}
}

@mannanfazil
please help me to solve this problem .

@rabin.samanta

Thank you for patience. Please check the following link with sample code which is related to your requirement.

FindAndHighlightText

Just update the following code, it will return only desired results. Hope, this helps.

Pattern regex = Pattern.compile("\\bhello\\b");
doc.getRange().replace(regex, "", options);

@mannanfazil
Pattern regex = Pattern.compile("\bhello\b");
this “\b” is not working in Linux server can you give me other solution.

@rabin.samanta

Kindly share screenshot of the following portion of code, where it shows undesired behavior just for reference.

@mannanfazil
it’s not showing any error process stack in this line.

@rabin.samanta

I hope you tried with the both, double slash “\\bhello\\b” and single slash “\bhello\b”. Hope, this will helps and also read the members of FindReplaceOptions class.

Please set the value of FindReplaceOptions.FindWholeWordsOnly property as true in your code. The true value indicates the oldValue must be a standalone word.