I’m trying to find specific words in a string and wrap those words with <b>. Below is what I have but I’m wondering whether there is a more effecient groovyish way to do this?
void testSomething() {
def myText = "The quick brown fox is very feisty today"
def BOUNDS = /\b/
def myWords = "quick very"
def words = myWords.tokenize(" ").join("|")
def regex = /$BOUNDS($words)$BOUNDS/
def found = ''
myText.eachMatch(regex) { match ->
found += match[0] + ' '
}
assert found == 'quick very '
def foundList = found.tokenize(" ")
def newList = []
myText.tokenize(" ").each {word ->
if (foundList.contains(word))
word = "<b>${word}</b>"
newList.add(word)
}
assert "The <b>quick</b> brown fox is <b>very</b> feisty today" == newList.join(" ")
}
Sure, just use
String.replaceAll().