Advertisement
Guest User

HighlightText

a guest
Mar 30th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. private static String highlightText(String html) {
  2. String regex = "(?<start><span>)(?<text>.*?)(?<end><\\/span>)";
  3. Pattern pattern = Pattern.compile(regex);
  4. Matcher matcher = pattern.matcher(html);
  5.  
  6. StringBuilder toReturn = new StringBuilder(html);
  7.  
  8.  
  9. while (matcher.find()) {
  10. String replacement = String.format("<b>%s</b>",
  11. matcher.group("text").replaceAll("&", ""));
  12. int start = toReturn.indexOf(matcher.group("text"));
  13. int end = start + matcher.group("text").length();
  14. toReturn.replace(start, end, replacement);
  15.  
  16. }
  17. return toReturn.toString();
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement