How to match words in a string that are wrapped in curly brackets using RegEx with Kotlin

1 Answer

0 votes
fun main() {
    val str = "This is a {string} with {multiple} {words} wrapped in curly brackets."

    // Define the RegEx pattern
    val regex = Regex("\\{([^}]+)\\}")

    // Find all matches
    val matches = regex.findAll(str).map { it.groupValues[1] }.toList()

    println("Matches: $matches")
}

  
     
/*
run:
  
Matches: [string, multiple, words]
 
*/

 



answered Mar 18 by avibootz
...