Updated March 29, 2023
Introduction to Kotlin Pattern Matching
In kotlin, pattern matching is the process of checking the datas whether it may be the specific sequence of the characters, tokens and even other data exists from among the other given datas the regular programming languages will make use of the regular expression like regex for pattern matching for to find and replace the matching pattern in the text or code with the another datas text and code parallelly it is used to determine whether the source files of the high-level languages it’s based on the syntactically correct also used to find and replace the matching pattern.
Syntax
The kotlin language has many default classes, methods & variables for implementing the application. Based on that pattern matching is one of the regular expression types using these regular expression objects the instances are called using the default methods implemented with the kotlin.text.Regex class.
fun main()
{
val varname = Regex(values)
varname.matches(values)
varname.matchEntire(values)
}
The above codes are the basic syntax for utilising the pattern method like matches(), metchEntire() these are the some default methods which are used for handling the regular expression values in the kotlin language.
How does Pattern Matching work in Kotlin?
- The kotlin language has used many default classes with methods like to implement the application. Among that kotlin pattern is one of the class of the regular expression it is the fundamental part of almost every programming language and kotlin no exception for it. Similar to the other language kotlin support regular expression with the help of Regex class an object of this class which represents the regular expression that can be used for the string matching purposes.
- Mainly the pattern matching is the process of checking whether the specific sequence of the characters, tokens existing datas among the given datas. The Regular programming languages make use of regular expressions for pattern matching for it helps to determine whether the source files of the high-level languages are syntactically correct and it helps to find with replace of the matched pattern in the text or code with another device values. Any type of web based and standalone application that can be supported using the search functionality and pattern matched with one way or another different ways depends on the user requirement.
- To work with regular expressions in kotlin we use the Regex(pattern: String) class and it is invoked with the default functions for used in the regex object call. Mostly the essential thing is to understand with the Regex functions is that the output results are based on matching the regex pattern and the input strings. Some type of functions it requires the full match while the rest of the things requires only in the partial match of values.
Examples of Kotlin Pattern Matching
Given below are the examples mentioned:
Example #1
Code:
class first {
fun mthd() = "Welcome To The Samsung Showroom"
fun mthd1() = "Welcome To The Panasonic Showroom"
fun mthd2() = "Welcome To The Onida Showroom"
fun mthd3() = "Welcome To The EAirtec Showroom"
fun mthd4() = "Welcome To The Airtel TV Showroom"
fun mthd5() = "Welcome To The Thomson Showroom"
fun mthd6() = "Welcome To The BPL Showroom"
fun mthd7() = "Welcome To The Vu Showroom"
fun mthd8() = "Welcome To The Mi Showroom"
fun mthd9() = "Welcome To The Videocon Showroom"
fun mthd10() = "Welcome To The Reliance Digital TV Showroom"
}
fun main()
{
println("Welcome To My Domain its the first example that related to the kotlin pattern matching concepts")
val x = Regex("F([ir]+)st?")
println(x.matches("January is the First Month"))
println(x.matches("First"))
println(x.matches("February is the Next of the First Month its the second month"))
println(x.matches("March is the Next of the Second Month its the third month"))
println(x.matches("April is the Next of the third Month its the fourth month"))
println(x.matches("May is the Next of the fourth Month its the fifth month"))
println(x.matches("June is the Next of the fifth Month its the sixth month"))
println(x.matches("July is the Next of the sixth Month its the seventh month"))
println(x.matches("August is the Next of the seventh Month its the eight month"))
println(x.matches("September is the Next of the eight Month its the ninth month"))
println(x.matches("October is the Next of the ninth Month its the tenth month"))
println(x.matches("November is the Next of the tenth its the eleventh month"))
println(x.matches("December is the Next of the eleventh Month its the twelth month"))
}
Output:
In the above example we created class as first and declared some functions like mthd(), mthd()1,,…mthd10(). For each method we have declared the values with string datatype. Next in main method() we declared the variable like x and its having the values using the Regular Expression class method. We can pass the parameter value like “F[(ir]+)st?”) by using this expression whenever the values must start with F and irst is the remaining words yet to complete. If suppose the remaining words are not same means it also return the false as the output console.
Example #2
Code:
val universities = "(SRM|Anna|MGR|Annamalai|Periyar|Kamarajar|Madras|Amritha|VIT|Vels|Bharath|Chitharambaram)"
fun getPattern() = """\d{2}\ ${universities}\ \d{4}"""
fun main(args:Array<String>) {
val inp = "[a-zA-Z0-9]+"
val inp2 = Regex(inp)
println(inp2.find("Wel_come")?.value)
inp2.findAll("Welcome__Wel-123").forEach {
println(it.value)
}
println(inp2.matches("Welcome To the VSR Group of Companies"))
println(inp2 matches "Welcome To SVR Group of Companies")
println(inp2.matches("Welcome__Wel"))
println(inp2.matchEntire("Welcome")?.value)
println(inp2.containsMatchIn("Welcome__Wel"))
println(inp2.containsMatchIn("!#$%"))
println("MGR University".matches(getPattern().toRegex()))
val x = """
Shoefabrik has reported an issue in the Mirror server where Samples Tab not loading for Supplier Portal user.
For this issue we have escalated to development team and they suggested to upgrade the SQL Service pack of the Mirror server to resolve this issue."""
val ptn = "\\w+".toRegex()
val wrds = ptn.findAll(x)
val total = wrds.count()
println("We can calculate the $total wrds")
wrds.forEach { result ->
println(result.value)
}
}
Output:
In the second example we declared the variable values with some sentences and applied the default methods like count(), Regex() and findAll() for to perform the user operations in the inputs. We can iterate the output values using the forEach loops for to calculate the total no of words and find the characters for the specified variable.
Conclusion
In kotlin language we used different classes and its methods for to built the application with more sophisticated. Like that kotlin regular expression is the class and it method like pattern() for to implement the words using the matches(), matchesEntire() and containsMatchIn() method for to perform the user operations in the kotlin.
Recommended Articles
This is a guide to Kotlin Pattern Matching. Here we discuss the introduction, syntax, and working of pattern matching in kotlin along with different examples. You may also have a look at the following articles to learn more –