W3cubDocs

/Kotlin

Destructured

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
class 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.



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
}

Properties

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

match

val match: MatchResult

Functions

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component1

operator fun component1(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component10

operator fun component10(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component2

operator fun component2(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component3

operator fun component3(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component4

operator fun component4(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component5

operator fun component5(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component6

operator fun component6(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component7

operator fun component7(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component8

operator fun component8(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

component9

operator fun component9(): String
Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)

toList

Returns destructured group values as a list of strings. First value in the returned list corresponds to the value of the first group, and so on.

fun toList(): List<String>

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-match-result/-destructured/index.html