Destructured
Provides components for destructuring assignment of group values.
component1 corresponds to the value of the first group, component2 — of the second, and so on.
If the group in the regular expression is optional and there were no match captured by that group, corresponding component value is an empty string.
Samples
import samples.*
fun main() {
//sampleStart
val inputString = "John 9731879"
val match = Regex("(\\w+) (\\d+)").find(inputString)!!
val (name, phone) = match.destructured
assertPrints(name, "John") // value of the first group matched by \w+
assertPrints(phone, "9731879") // value of the second group matched by \d+
// group with the zero index is the whole substring matched by the regular expression
assertPrints(match.groupValues, "[John 9731879, John, 9731879]")
val numberedGroupValues = match.destructured.toList()
// destructured group values only contain values of the groups, excluding the zeroth group.
assertPrints(numberedGroupValues, "[John, 9731879]")
//sampleEnd
}
Functions
component1
Link copied to clipboard
component10
Link copied to clipboard
component2
Link copied to clipboard
component3
Link copied to clipboard
component4
Link copied to clipboard
component5
Link copied to clipboard
component6
Link copied to clipboard
component7
Link copied to clipboard
component8
Link copied to clipboard
component9
Link copied to clipboard
Properties
match
Link copied to clipboard