docx-word-replacer
docx-word-replacer copied to clipboard
use regex instead of contains()
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
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;
}