1

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]"

2
  • 3
    You can use a regular expression noTPL.replaceAll("\\[tpl\\].*?\\[/tpl\\]", "") Commented Apr 30, 2019 at 4:33
  • I'm noob... how do I mark this response as the correct answer? Also, it would be input.replaceAll(...) based on my code. Commented Apr 30, 2019 at 5:07

2 Answers 2

2

You can use regular expressions, but if you want a "by steam" version (which I think can be clearer), here's an attempt. Note the use of indexOfSlice and patch to simplify things.

  val input = "Today is Wednesday.[tpl] Let's go fishing.[/tpl] Then let's go to the park.[tpl] But it is cold out.[/tpl] Nevermind."

  def stripTags(input: String): String = {
    val start = input.indexOfSlice("[tpl]")
    val end = input.indexOfSlice("[/tpl]")

    if (start != -1 && end != -1) {
      // we have a pair
      val actualEnd = end + "[/tpl]".length

      stripTags(input.patch(start, "", actualEnd - start))
    } else
      input
  }

 stripTags(input) // "Today is Wednesday. Then let's go to the park. Nevermind."
Sign up to request clarification or add additional context in comments.

Comments

2

As suggested by Harald in his comment you can use a regular expression.

Assuming your input is:

val input = "Today is Wednesday.[tpl] Let's go fishing.[/tpl]."

you can get the expected String using the following method:

val noTPL = input.replaceAll("\\[tpl\\]|\\[/tpl\\].*?", "")

For a matter of completeness, please check the documentation of replaceAll method here.

1 Comment

The trailing bracket ] does not need to be escaped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.