find
Требования к платформе и версии: JVM (1.0), JS (1.1), Native (1.3)
fun find( input: CharSequence, startIndex: Int = 0 ): MatchResult?
Возвращает первое совпадение регулярного выражения в входных данных, начиная с указанного индекса начала поиска.
fun main(args: Array<String>) {
//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
}Параметры
startIndex - Индекс для начала поиска, по умолчанию 0. Должен быть не меньше нуля и не больше input.length()
Исключения
IndexOutOfBoundsException - если индекс начала поиска меньше нуля или больше длины входной последовательности символов.
Возвращаемое значение Экземпляр MatchResult, если совпадение найдено, или null в противном случае.
© 2010–2022 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/-regex/find.html