How to replace multiple consecutive question marks (?) with a single question mark in Scala

1 Answer

0 votes
import scala.util.matching.Regex

object ReplaceQuestionMarks extends App {
  val str = "What??? Why?? How?????"

  // Define regex pattern to match one or more '?'
  val pattern: Regex = "\\?+".r

  // Replace all matches with a single '?'
  val result = pattern.replaceAllIn(str, "?")

  println(result)
}




/*
run:
 
What? Why? How?
 
*/
 

 



answered Jul 24, 2025 by avibootz
...