简体   繁体   English

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

[英]match a timestamp based on regex pattern matching scala

I wrote the following code : 我写了以下代码:

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"

This function is supposed to be applied to strings having the following format: 2018-08-22T19:10:53.094Z 该功能应该应用于具有以下格式的字符串: 2018-08-22T19:10:53.094Z

When I call the function : 当我调用函数时:

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

Your pattern, for all its deficiencies, does work. 尽管存在所有缺陷,但您的模式仍然有效。 You just have to unanchor it. 您只需要固定它即可。

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)

But the comment from @CAustin is correct, you could just let the Java LocalDateTime API handle all the heavy lifting. 但是@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)

Now you have access to all the data without actually saving it to a Map . 现在,您可以访问所有数据,而无需实际将其保存到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

Your pattern matches only strings that look exactly like yyyy-mm-ddThh:mm , while the one you are testing against has milliseconds and a Z at the end. 您的模式仅匹配看起来完全yyyy-mm-ddThh:mm字符串,而您要测试的字符串则以毫秒为单位,末尾为Z。

You can append .* at the end of your pattern to cover strings that have additional characters at the end. 您可以在模式的末尾附加.* ,以覆盖末尾带有其他字符的字符串。

In addition, let me show you a more idiomatic way of writing your code: 另外,让我向您展示一种更惯用的代码编写方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM