scala學習:提取器
unapply 與模式匹配
object EMail {
def apply(user: String, domain: String): String = user + "@" + domain
def unapply(string: String): Option[(String, String)] = {
val strings: Array[String] = string.split("@")
if (strings.length == 2) Some(strings(0), strings(1)) else None
}
}
def una(x: String): Unit = x match {
case EMail(user, domain) => println(s"user: $user, domain: $domain")
case _ => println("is not a email")
}
una("ljk@qq.com")
una("taobao.com")
正則表達式
test("regex test "){
val decimal = """(-)?(\d+)(\.\d*)?""".r// 可選-,加上一個數字或者多個數字,加上可選的 可能小數點和0個或者多個數字
val decimal(a,b,c) = "-1.23"
println(s"a: $a, b: $b, c: $c")
}