canislupus8
canislupus8

Reputation: 11

remove all substrings matching pattern within a string in scala

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

Upvotes: 1

Views: 80

Answers (2)

P3trur0
P3trur0

Reputation: 3225

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.

Upvotes: 2

The Archetypal Paul
The Archetypal Paul

Reputation: 41749

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

Upvotes: 2

Related Questions