toRegex

Converts the string into a regular expression Regex with the default options.

inline fun String.toRegex(): Regex(source)
fun main() {
    val regex = "\\d{4}".toRegex()          // 4-digit number pattern
    val text  = "The year 2024 is coming."

    val match = regex.find(text)
    println(match?.value)                    // prints: 2024
}

Source