Spec-Zone.ru › Kotlin 2

destructured

kotlin-stdlib/kotlin.text/MatchResult/destructured

Начиная с Kotlin: 1.0

open val destructured: MatchResult.Destructured

Экземпляр обёртки MatchResult.Destructured, предоставляющий компоненты для деструктурирующего присваивания значений групп.

component1 соответствует значению первой группы, component2 — второй и так далее.

Примеры

import kotlin.test.*

fun main() { 
   //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
}

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API