docx-word-replacer icon indicating copy to clipboard operation
docx-word-replacer copied to clipboard

use regex instead of contains()

Open DavidRobertKeller opened this issue 2 years ago • 1 comments

Your java lib is really nice ! But you use "contains()" so to can't make difference between $rabbit and $rabbitYellow during replacement. @see stackoverflow.com/questions/42904361/… So you can update source with : Pattern.compile("(?<!\S)" + Pattern.quote(bookmark) + "(?!\S)");

best ! David

DavidRobertKeller avatar Jun 01 '22 09:06 DavidRobertKeller

For example :

    public static boolean containsWord(String text, String bookmark) {
    	boolean contains = false;
		
    	if (text.contains(bookmark)) {
    		final Pattern pattern = Pattern.compile(Pattern.quote(bookmark) + "([^A-Za-z0-9]|$)");
    		final Matcher matcher = pattern.matcher(text);
    		while (matcher.find()) {
    			contains = true;
    			break;
    		}
    	}
    	
    	return contains;
    }

DavidRobertKeller avatar Jun 01 '22 12:06 DavidRobertKeller