John John - 1 month ago 6
Scala Question

String format using regex in Scala

I have a String in the format of "20150403" and I want to convert it into the format of string like "2015-04-03". Is there any way using regular expression and string format in Scala?

Answer

Here is a sample

val date = """(\d{4})(\d{2})(\d{2})""".r
val date(year, month, day) = "20150402"
println(s"$year-$month-$day") // 2015-04-02
Comments