Kotlin模式匹配简介

834 阅读4分钟

Kotlin Pattern Matching

Kotlin模式匹配简介

在Kotlin中,模式匹配是检查数据的过程,它是否可能是字符、标记甚至其他数据的特定序列,从其他给定的数据中存在,正则编程语言将利用正则表达式如regex进行模式匹配,以寻找和替换文本或代码中的匹配模式与另一个数据文本和代码平行,它被用来确定高级语言的源文件是否在语法上正确,它也被用来寻找和替换匹配模式。

语法

kotlin语言有许多默认的类、方法和变量用于实现应用程序。基于此,模式匹配是正则表达式类型之一,使用这些正则表达式对象的实例被调用,使用kotlin.text.Regex类实现的默认方法。

fun main()
{
val varname = Regex(values)
varname.matches(values)
varname.matchEntire(values)
}

上述代码是利用模式方法的基本语法,如matches()、metchEntire()这些是一些默认的方法,用于处理kotlin语言中的正则表达式值。

模式匹配如何在Kotlin中工作?

  • Kotlin语言使用了许多默认类和方法来实现应用程序。其中,Kotlin模式是正则表达式类之一,它几乎是每一种编程语言的基本组成部分,Kotlin也不例外。与其他语言类似,kotlin也支持正则表达式,借助于Regex类,该类的对象表示正则表达式,可用于字符串匹配目的。
  • 主要的模式匹配是检查给定数据中是否存在特定的字符序列、标记数据的过程。正则编程语言利用正则表达式进行模式匹配,因为它有助于确定高级语言的源文件在语法上是否正确,并有助于在文本或代码中找到与另一个设备值相匹配的模式的替换。任何类型的基于网络的和独立的应用程序都可以使用搜索功能和模式匹配来支持,这种方式或另一种不同的方式取决于用户需求。
  • 为了在kotlin中使用正则表达式,我们使用Regex(pattern: String)类,它被调用的默认函数用于调用regex对象。最重要的是要理解Regex函数,其输出结果是基于对regex模式和输入字符串的匹配。某些类型的函数需要完全匹配,而其余的只需要部分匹配的值。

Kotlin模式匹配的例子

下面是提到的例子。

例子#1

代码

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"))
}

输出

Kotlin Pattern Matching 1

在上面的例子中,我们首先创建了类,并声明了一些函数,如mthd(), mthd()1,,...mthd10()。对于每个方法,我们都声明了字符串数据类型的值。接下来在main method()中我们声明了像x这样的变量,并使用正则表达式类方法对其进行了取值。我们可以通过使用这个表达式来传递参数值,如 "F[(ir]+)st?"),只要值必须以F开头,第一是剩下的字还没有完成。如果假设剩余的字不一样,它也会返回false作为输出控制台。

例子 #2

代码

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)
}
}

输出

with some sentences and applied the default methods

Kotlin Pattern Matching 3

在第二个例子中,我们用一些句子声明了变量值,并应用了默认的方法,如count(), Regex()和findAll()来执行输入中的用户操作。我们可以使用forEach循环来迭代输出值,以计算总字数并找到指定变量的字符。

总结

在kotlin语言中,我们使用不同的类和它的方法来建立更复杂的应用程序。如kotlin正则表达式是一个类,它的方法如pattern()用于实现单词,使用matches()、matchesEntire()和containsMatchIn()方法来执行kotlin中的用户操作。