How to replace the characters !@#$%^*_+\= in a string using RegEx with Scala

1 Answer

0 votes
object Main extends App {
  val inputText = "The!quick@brown#fox$jumps%^over*_the+\\lazy=dog."
  val pattern = "[!@#$%^*_+=\\\\]"
  val replacement = " "

  // Perform regex replacement
  val result = inputText.replaceAll(pattern, replacement)

  println(s"Original: $inputText")
  println(s"Modified: $result")
}


 
/*
run:

Original: The!quick@brown#fox$jumps%^over*_the+\lazy=dog.
Modified: The quick brown fox jumps  over  the  lazy dog.

*/

 



answered Jun 11, 2025 by avibootz
...