<a href='/door.html'>Our door</a>
String replace will break the structure of the XHTML:
<a href='/<strong>door</strong>.html'>Our <strong>door</strong></a>
Another solution is to use regular expression: ((<.[^>]*>))|(\bdoor\b). This regexp has two parts:
- ((<.[^>]*>)) - matches any tag
- (\bdoor\b)- matches word 'door'
// rawContent contains input string
Pattern pattern = Pattern.compile("(<.[^>]*>)|(\\bdoor\\b)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher matcher = pattern.matcher(rawContent);
matcher.reset();
int lastMatchEnd = 0;
boolean result = matcher.find();
if (result) {
StringBuffer sb = new StringBuffer();
do {
// Add text before match
sb.append(rawContent.substring(lastMatchEnd, matcher.start()));
lastMatchEnd = matcher.end();
String temp = matcher.group();
if (!temp.startsWith("<")) {
sb.append("").append(temp).append("");
} else {
sb.append(temp);
}
result = matcher.find();
} while (result);
sb.append(rawContent.substring(lastMatchEnd));
return sb.toString();
}
return rawContent;
No comments:
Post a Comment