根据正则表达式模式匹配scala匹配时间戳

时间:2018-08-28 20:15:50

标签: regex scala

我写了以下代码:

val reg = "([\\d]{4})-([\\d]{2})-([\\d]{2})(T)([\\d]{2}):([\\d]{2})".r
val dataExtraction: String => Map[String, String] = {
  string: String => {
    string match {
      case reg(year, month, day, symbol, hour, minutes) =>
                 Map(YEAR -> year, MONTH -> month, DAY -> day, HOUR -> hour)
      case _  => Map(YEAR -> "", MONTH -> "", DAY -> "", HOUR -> "")
    }
  }
}
val YEAR = "YEAR"
val MONTH = "MONTH"
val DAY = "DAY"
val HOUR = "HOUR"

该功能应该应用于具有以下格式的字符串:2018-08-22T19:10:53.094Z

当我调用函数时:

dataExtractions("2018-08-22T19:10:53.094Z")

2 个答案:

答案 0 :(得分:2)

您的模式尽管存在所有缺陷,但确实有效。您只需要固定它即可。

val reg = "([\\d]{4})-([\\d]{2})-([\\d]{2})(T)([\\d]{2}):([\\d]{2})".r.unanchored
. . .
dataExtraction("2018-08-22T19:10:53.094Z")
//res0: Map[String,String] = Map(YEAR -> 2018, MONTH -> 08, DAY -> 22, HOUR -> 19)

但是@CAustin的评论是正确的,您可以让Java LocalDateTime API处理所有繁重的工作。

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter._

val dt = LocalDateTime.parse("2018-08-22T19:10:53.094Z", ISO_DATE_TIME)

现在,您可以访问所有数据,而无需实际将其保存到Map

dt.getYear        //res0: Int = 2018
dt.getMonthValue  //res1: Int = 8
dt.getDayOfMonth  //res2: Int = 22
dt.getHour        //res3: Int = 19
dt.getMinute      //res4: Int = 10
dt.getSecond      //res5: Int = 53

答案 1 :(得分:0)

您的模式仅匹配看上去与yyyy-mm-ddThh:mm完全相似的字符串,而您要测试的字符串则以毫秒为单位,末尾为Z。

您可以在模式的末尾附加.*,以覆盖末尾带有其他字符的字符串。

此外,让我向您展示一种更惯用的代码编写方式:

// Create a type for the data instead of using a map.
case class Timestamp(year: Int, month: Int, day: Int, hour: Int, minutes: Int)

// Use triple quotes to avoid extra escaping.
// Don't capture parts that you will not use.
// Add .* at the end to account for milliseconds and timezone.
val reg = """(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}).*""".r

// Instead of empty strings, use Option to represent a value that can be missing.
// Convert to Int after parsing.
def dataExtraction(str: String): Option[Timestamp] = str match {
  case reg(y, m, d, h, min) => Some(Timestamp(y.toInt, m.toInt, d.toInt, h.toInt, min.toInt))
  case _                    => None
}

// It works!
dataExtraction("2018-08-22T19:10:53.094Z")  // => Some(Timestamp(2018,8,22,19,10))