groupValues

Common
JVM
JS
Native
1.0
abstract val groupValues: List<String>

A list of matched indexed group values.

This list has size of groupCount + 1 where groupCount is the count of groups in the regular expression. Groups are indexed from 1 to groupCount and group with the index 0 corresponds to the entire match.

If the group in the regular expression is optional and there were no match captured by that group, corresponding item in groupValues is an empty string.



fun main(args: Array<String>) {
//sampleStart
val inputString = "John 9731879"
val match = Regex("(\\w+) (\\d+)").find(inputString)!!
val (name, phone) = match.destructured

println(name) // John     // value of the first group matched by \w+
println(phone) // 9731879 // value of the second group matched by \d+

// group with the zero index is the whole substring matched by the regular expression
println(match.groupValues) // [John 9731879, John, 9731879]

val numberedGroupValues = match.destructured.toList()
// destructured group values only contain values of the groups, excluding the zeroth group.
println(numberedGroupValues) // [John, 9731879]
//sampleEnd
}