Spec-Zone.ru › Kotlin 2

Destructured

kotlin-stdlib/kotlin.text/MatchResult/Destructured

Версия Kotlin: 1.0

class 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
}

Свойства

match

Версия Kotlin: 1.0

val match: MatchResult

Функции

component1

Версия Kotlin: 1.0

inline operator fun component1(): String
component10

Версия Kotlin: 1.0

inline operator fun component10(): String
component2

Версия Kotlin: 1.0

inline operator fun component2(): String
component3

Версия Kotlin: 1.0

inline operator fun component3(): String
component4

Версия Kotlin: 1.0

inline operator fun component4(): String
component5

Версия Kotlin: 1.0

inline operator fun component5(): String
component6

Версия Kotlin: 1.0

inline operator fun component6(): String
component7

Версия Kotlin: 1.0

inline operator fun component7(): String
component8

Версия Kotlin: 1.0

inline operator fun component8(): String
component9

Версия Kotlin: 1.0

inline operator fun component9(): String
toList

Версия Kotlin: 1.0

fun toList(): List<String>

Возвращает значения групп в виде списка строк. Первое значение в возвращаемом списке соответствует значению первой группы и так далее.

© 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/index.html

Spec-Zone.ru

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