Example of matchEntire() function in Kotlin

Learn about the matchEntire() function in Kotlin, and demonstrate the example of the matchEntire() function.
Submitted by IncludeHelp, on March 25, 2022

matchEntire() Function

The matchEntire() function is used to match the entire given string against the pattern and returns an instance of MatchResult if the entire input string matches; null, otherwise.

Syntax:

fun matchEntire(input: CharSequence): MatchResult?

Kotlin program to demonstrate the example of matchEntire() function

// Example of matchEntire() function
fun main()
{
	// Check entire string match
	var pattern = Regex("Hello?")
	println(pattern.matchEntire("Hello")?.value)
	println(pattern.matchEntire("Helllllo")?.value)
	println(pattern.matchEntire("Hello, World!")?.value)

	pattern = Regex("""\D+""")
	println(pattern.matchEntire("Hello")?.value)
	println(pattern.matchEntire("Hello, Boss!")?.value)
	println(pattern.matchEntire("Hello101")?.value)
}

Output:

Hello
null
null
Hello
Hello, Boss!
null

Kotlin Regular Expression (Regex) Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.