Scala - 正则表达式匹配例子

时间:2022-11-10 05:58:03

壹Try胜仟言

别忘了 import scala.util.matching._

 

scala> var s = "a_b_c_d_e"
s: String = a_b_c_d_e

scala> val regex = new Regex("^([^_]*)_([^_]*)_([^_]*)_(.*)$")
regex: scala.util.matching.Regex = ^([^_]*)_([^_]*)_([^_]*)_(.*)$

scala> val regex(time, user,cust,ss ) = s
time: String = a
user: String = b
cust: String = c
ss: String = d_e