/**
* Remove all parentheses and the text inside them.
*
* @param text The input string
* @return The cleaned string
*/
def removeParenthesesWithContent(text: String): String = {
// Remove parentheses and everything inside them
val cleaned: String = text.replaceAll("\\([^)]*\\)", " ")
// Collapse multiple spaces into one
val collapsed: String = cleaned.split("\\s+").mkString(" ")
// Final trim of leading/trailing spaces
collapsed.trim
}
@main def run(): Unit = {
val str: String =
"(An) API (API) (is a) (connection) connects (between) computer programs"
val output: String = removeParenthesesWithContent(str)
println(output)
}
/*
run:
API connects computer programs
*/