Regular Expressions in Kotlin

Kotlin offers built-in support for regular expressions. Here are some examples of common regexp-related tasks in Kotlin.

import kotlin.text.Regex

fun main() {
    // This tests whether a pattern matches a string.
    val match = Regex("p([a-z]+)ch").matches("peach")
    println(match)

    // For other regexp tasks, we'll create a Regex object.
    val r = Regex("p([a-z]+)ch")

    // Here's a match test like we saw earlier.
    println(r.matches("peach"))

    // This finds the match for the regexp.
    println(r.find("peach punch")?.value)

    // This also finds the first match but returns the
    // start and end indexes for the match instead of the
    // matching text.
    val matchResult = r.find("peach punch")
    println("idx: ${matchResult?.range}")

    // The groups property includes information about
    // both the whole-pattern matches and the submatches
    // within those matches.
    matchResult?.let {
        println(listOf(it.value) + it.groupValues.drop(1))
    }

    // This will return information about the
    // indexes of matches and submatches.
    matchResult?.let {
        println(listOf(it.range.first, it.range.last + 1) + 
                it.groups.drop(1).map { group -> group?.range?.let { range -> listOf(range.first, range.last + 1) } }.filterNotNull().flatten())
    }

    // To find all matches for a regexp.
    println(r.findAll("peach punch pinch").map { it.value }.toList())

    // These variants are available for the other
    // functions we saw above as well.
    println("all: ${r.findAll("peach punch pinch").map { 
        listOf(it.range.first, it.range.last + 1) + 
        it.groups.drop(1).map { group -> group?.range?.let { range -> listOf(range.first, range.last + 1) } }.filterNotNull().flatten()
    }.toList()}")

    // Providing a limit to these functions will limit the number
    // of matches.
    println(r.findAll("peach punch pinch").take(2).map { it.value }.toList())

    // Our examples above had string arguments and used
    // names like matches. We can also provide
    // ByteArray arguments and use containsMatchIn.
    println(r.containsMatchIn("peach".toByteArray().toString(Charsets.UTF_8)))

    // When creating global variables with regular
    // expressions you can use the Regex constructor.
    val globalR = Regex("p([a-z]+)ch")
    println("regexp: ${globalR.pattern}")

    // The Regex class can also be used to replace
    // subsets of strings with other values.
    println(r.replace("a peach", "<fruit>"))

    // The transform function allows you to transform matched
    // text with a given function.
    val input = "a peach"
    val output = r.replace(input) { it.value.uppercase() }
    println(output)
}

Running this program would produce:

true
true
peach
idx: 0..4
[peach, ea]
[0, 5, 1, 3]
[peach, punch, pinch]
all: [[0, 5, 1, 3], [6, 11, 7, 9], [12, 17, 13, 15]]
[peach, punch]
true
regexp: p([a-z]+)ch
a <fruit>
a PEACH

For a complete reference on Kotlin regular expressions, check the Regex class documentation in the Kotlin standard library.