I am trying to remove all substrings that start with "[tpl]" and end with "[/tpl]" within a string using Scala. There can be multiple instances of these substrings within the same string.
Example string: "Today is Wednesday.[tpl] Let's go fishing.[/tpl] Then let's go to the park.[tpl] But it is cold out.[/tpl] Nevermind."
Expected output: "Today is Wednesday. Then let's go the the park. Nevermind."
var noTPL = ListBuffer[Char]()
var foundTPL = false
input.foreach(char => {
if (input.indexOf(char) < input.length() - 5 && input.substring(input.indexOf(char), input.indexOf(char) + 5) == "[tpl]") {
foundTPL = true
}
if (input.indexOf(char) < input.length() - 6 && input.substring(input.indexOf(char), input.indexOf(char) + 6) == "[/tpl]") {
foundTPL = false
println("FOUND [/tpl]")
}
if (!foundTPL) {
noTPL += char
}
})`
This code finds the "[tpl]" but never finds the "[/tpl]"
noTPL.replaceAll("\\[tpl\\].*?\\[/tpl\\]", "")