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

1 Answer

0 votes
fun main() {
    val inputText = "The!quick@brown#fox\$jumps%^over*_the+\\lazy=dog."
    val pattern = Regex("[!@#$%^*_+=\\\\]")
    val replacement = " "

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

    println("Original: $inputText")
    println("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
...