Spec-Zone.ru › Kotlin 2

find

kotlin-stdlib/kotlin.text/Regex/find

Требования к платформе и версии: Common (1.0), JS (1.1), JVM (1.0), Native (1.3), Wasm-JS (1.8), Wasm-WASI (1.8)

expect fun find(input: CharSequence, startIndex: Int = 0): MatchResult?

Возвращает первое совпадение регулярного выражения во входной последовательности input, начиная с указанного startIndex.

Возвращаемое значение

Экземпляр MatchResult, если совпадение найдено, или null в противном случае.

Параметры

startIndex

Индекс, с которого начинается поиск; по умолчанию — 0. Должен быть не меньше нуля и не больше input.length()

Исключения

IndexOutOfBoundsException

если startIndex меньше нуля или больше длины символьной последовательности input.

Примеры

import kotlin.test.*

fun main() { 
   //sampleStart 
   val inputString = "to be or not to be"
val regex = "to \\w{2}".toRegex()
// If there is matching string, then find method returns non-null MatchResult
val match = regex.find(inputString)!!
println(match.value) // to be
println(match.range) // 0..4

val nextMatch = match.next()!!
println(nextMatch.range) // 13..17

val regex2 = "this".toRegex()
// If there is no matching string, then find method returns null
println(regex2.find(inputString)) // null

val regex3 = regex
// to be or not to be
//              ^^^^^
// Because the search starts from the index 2, it finds the last "to be".
println(regex3.find(inputString, 2)!!.range) // 13..17 
   //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/-regex/find.html

Spec-Zone.ru

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