MatchResult.destructured所在位置是kotlin.text.MatchResult.destructured,其相关用法介绍如下。

用法:

open val destructured: Destructured

MatchResult.Destructured 包装器的一个实例,提供用于解构组值分配的组件。

component1 对应于第一组的值,component2 对应于第二组的值,依此类推。

例子:



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
}

输出:

John
9731879
[John 9731879, John, 9731879]
[John, 9731879]