KT-20357 - Add sample for Regex.findAll()
This commit is contained in:
@@ -53,6 +53,8 @@ expect class Regex {
|
||||
|
||||
/**
|
||||
* Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
|
||||
*
|
||||
* @sample samples.text.Regexps.findAll
|
||||
*/
|
||||
fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult>
|
||||
|
||||
|
||||
@@ -74,7 +74,10 @@ public actual class Regex actual constructor(pattern: String, options: Set<Regex
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun find(input: CharSequence, startIndex: Int = 0): MatchResult? = nativePattern.findNext(input.toString(), startIndex)
|
||||
|
||||
/** Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
|
||||
/**
|
||||
* Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
|
||||
*
|
||||
* @sample samples.text.Regexps.findAll
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> =
|
||||
|
||||
@@ -121,6 +121,8 @@ internal constructor(private val nativePattern: Pattern) : Serializable {
|
||||
|
||||
/**
|
||||
* Returns a sequence of all occurrences of a regular expression within the [input] string, beginning at the specified [startIndex].
|
||||
*
|
||||
* @sample samples.text.Regexps.findAll
|
||||
*/
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun findAll(input: CharSequence, startIndex: Int = 0): Sequence<MatchResult> =
|
||||
|
||||
@@ -45,4 +45,13 @@ class Regexps {
|
||||
// Because the search starts from the index 2, it finds the last "to be".
|
||||
assertPrints(regex3.find(inputString, 2)!!.range, "13..17")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun findAll() {
|
||||
val text = "Hello Alice. Hello Bob. Hello Eve."
|
||||
val regex = Regex("Hello (.*?)[.]")
|
||||
val matches = regex.findAll(text)
|
||||
val names = matches.map { it.groupValues[1] }.joinToString()
|
||||
assertPrints(names, "Alice, Bob, Eve")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user