Home »
Kotlin »
Kotlin Programs
Example of matches() function in Kotlin
Learn about the matches() function in Kotlin, and demonstrate the example of the matches() function.
Submitted by IncludeHelp, on March 25, 2022
matches() Function
The matches() function returns true if this char sequence matches the given regular expression.
Syntax:
infix fun CharSequence.matches(regex: Regex): Boolean
Kotlin program to demonstrate the example of matches() function
// Example of matches() function
fun main()
{
// Check entire string match
val pattern = Regex("He([ll]+)o?")
println(pattern.matches("Hello"))
println(pattern.matches("Hellllllllllllllllo"))
println(pattern.matches("HelloWorldHello"))
}
Output:
true
true
false
Explanation:
In the first two statements, strings contain the given pattern and in the third statement, the string doesn't contain the given pattern.
Kotlin Regular Expression (Regex) Programs »