Menu
Coddy logo textTech

Matching Text Patterns

Part of the Logic & Flow section of Coddy's Kotlin journey. Lesson 48 of 63.

A Regex describes a text pattern. [A-Z] matches one uppercase ASCII letter, [0-9] matches one digit, {2} requires exactly two repetitions and + requires one or more. matches checks the entire input; a matching substring alone is insufficient.

Inside main:

val code = Regex("[A-Z]{2}[0-9]{3}")
println(code.matches("AB123"))

The whole input contains exactly two uppercase letters followed by three digits.

Regex.matches requires the entire input to satisfy the pattern.

challenge icon

Challenge

Medium

Complete validCode with parameters text: String. Return whether text consists of exactly three uppercase ASCII letters followed by exactly two ASCII digits, with no spaces or other characters. Use Regex.matches.

Unless stricter bounds are stated above, collections contain at most 100 elements at each level, and integer arguments and integer collection values are between -1000 and 1000. Text supplied for parsing can include invalid or out-of-range representations as described.

Return a value of type Boolean. Keep the supplied input/output code. It reads scalar arguments one per line; a list starts with its count followed by its elements, and a map starts with its entry count followed by each key and value. Nested lists repeat the count-and-elements format for each row. The supplied main prints the return value followed by one newline. Lists use Kotlin's standard bracketed format; print no additional labels.

Try it yourself

fun validCode(text: String): Boolean {
    // Write your solution here.
    return false
}
fun main() {
    val text = readLine().orEmpty()
    println(validCode(text))
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow

Practice on your own: Kotlin playground