Regex 基于正则表达式模式匹配scala匹配时间戳

Regex 基于正则表达式模式匹配scala匹配时间戳,regex,scala,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) =>

我编写了以下代码:

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")

你的模式尽管有缺陷,但确实有效。你只要把它解开就行了

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)
现在,您可以访问所有数据,而无需实际将其保存到
地图

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

你的模式尽管有缺陷,但确实有效。你只要把它解开就行了

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)
现在,您可以访问所有数据,而无需实际将其保存到
地图

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

您的模式只匹配看起来与
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))

您的模式只匹配看起来与
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))

尝试一下为什么不使用Java的
Date
类呢?也没有理由把单个字符放在方括号内。试试为什么不使用Java的
Date
类呢?此外,没有理由将单个字符放在方括号内。