在 Scala 中,matches 函数用于告知此字符串是否匹配给定的正则表达式。
boolean matches(String regex)
参数 | 描述 |
---|---|
regex | 正则表达式 |
如果匹配,则返回 true,否则,返回 false。
使用 matches 函数,判断字符串正则匹配
object HaiCoder {
def main(args: Array[String]) : Unit = {
println("嗨客网(www.haicoder.net)!\n")
var str1 = "Hello,HaiCoder!"
var match1 = str1.matches(".*Coder!")
var match2 = str1.matches("H.")
println("match1 =", match1, "match2 =", match2)
}
}
程序运行后,控制台输出如下:
我们使用了 matches 函数可以判断某个字符串是否能够匹配某个正则表达式。
在 Scala 中,matches 函数用于告知此字符串是否匹配给定的正则表达式。