How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Kotlin

1 Answer

0 votes
fun main() {
    // Declare the regex pattern
    val pattern = Regex("^htt+p$")

    // Test strings
    val testStrings = listOf("http", "htttp", "httttp", "httpp", "htp")

    // Check matches
    for (test in testStrings) {
        val matches = pattern.matches(test)
        println("Matches \"$test\": $matches")
    }
}


// Matches "httpp": True or false, depending on how matches() method works
 
 
/*
run:
 
Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": false
Matches "htp": false
 
*/

 



answered May 16, 2025 by avibootz
...