【问题标题】:issue extracting date part from various formats in an unstructured text从非结构化文本中的各种格式中提取日期部分的问题
【发布时间】:2017-09-27 16:12:38
【问题描述】:

我正在尝试从一堆非结构化文本中仅提取日期部分。

问题是,日期可以是以下任何一种格式:

  • 一月。 2017 年 1 月 16 日或 16 日(2017 年 1 月 16 日)
  • 2017 年 1 月 2 日
  • 02/01/2017 (dd/mm/yyyy)
  • 01/02/2017 (mm/dd/yyyy)
  • 01-02-17 (mm-dd-yy)

示例文本:

x <- "There is a date which is Jan 2, 2017. Here is another date example 02/01/2017. This is third example date type [01/02/17]. This is fourth example date Jan. 16 and finally one more example is 01-02-2017"

我正在尝试的是其他选项之一(来自this 答案中的示例):

gsub(".*[(]|[)].*", "", string)

还有其他普遍的可能性吗?

【问题讨论】:

  • “Januray”是错字还是您需要考虑的实际情况?
  • 可能是THIS 可以作为一个开始。
  • 当您的示例文本不包含任何括号时,我不确定您为什么要尝试匹配括号。其次,用方括号转义特殊字符通常是不好的做法,因为这也是您创建字符集的方式,并且某些特殊字符不会以这种方式转义或具有不同的含义(例如^)。只需使用`\`。
  • @KenS。感谢您指出。是的,它是 Typro。
  • @CAustin - 实际上这只是一个大文档的示例文本,也有括号。

标签: r regex datetime pattern-matching


【解决方案1】:

首先,在不知道日期格式的情况下,对于这种情况,02/03/2002 您无法判断一天是一天还是一个月是一个月......如果年份也可以是 2 位数... 例如 dd/mm/yy 或 yy/mm/dd 或 mm/yy/dd ... 你不能说哪个是日,哪个是月,哪个是年...

考虑到所有这些事情...可能有来自第三方的字符串,您可能无法确定格式...因此没有解决方案可以保证为你。

但是可以识别您提到的所有数字模式。以下解决方案将为您提供三组。您将获得您在组 1,2 和 3 中提到的所有格式的日期的三个部分。您将不得不分析/猜测一种方法来确定哪一个是天,哪一个是月份,哪一个是年份。这不能被正则表达式覆盖。

考虑到所有这些事实,您可以尝试以下正则表达式:

((?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z ]*\.?)|(?:\d{1,2}))[\/ ,-](\d{1,2})(?:[\/ ,-]\s*(\d{4}|\d{2}))?

Regex 101 Demo

样本来源(run here):

library(stringr)
str<-"Jan. 16  bla bla bla Jan 16 2017 bla bla bla January 2, 2017 bla bla bla 02/01/2017 bla bla bla 01/02/2017 bla bla bla 01-02-17 bla bla bla jan. 16 There is a date which is Jan 2, 2017. Here is another date example 02/01/2017. This is third example date type [01/02/17]. This is fourth example date Jan. 16 and finally one more example is 01-02-2017"
patt <- "(?i)((?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z ]*\\.?)|(?:\\d{1,2}))[\\/ ,-](\\d{1,2})(?:[\\/ ,-]\\s*(\\d{4}|\\d{2}))?"
result<-str_match_all(str,patt)
result

样本输出:

      [,1]              [,2]      [,3] [,4]  
 [1,] "Jan. 16"         "Jan."    "16" ""    
 [2,] "Jan 16 2017"     "Jan"     "16" "2017"
 [3,] "January 2, 2017" "January" "2"  "2017"
 [4,] "02/01/2017"      "02"      "01" "2017"
 [5,] "01/02/2017"      "01"      "02" "2017"
 [6,] "01-02-17"        "01"      "02" "17"  
 [7,] "jan. 16"         "jan."    "16" ""    
 [8,] "Jan 2, 2017"     "Jan"     "2"  "2017"
 [9,] "02/01/2017"      "02"      "01" "2017"
[10,] "01/02/17"        "01"      "02" "17"  
[11,] "Jan. 16"         "Jan."    "16" ""    
[12,] "01-02-2017"      "01"      "02" "2017"

【讨论】:

  • 里兹万,谢谢。但是这有什么关系呢?例如,2002 年 2 月 2 日可以写成 02/02/2002(dd/mm/yyyy 或 mm/dd/yyyy)或 02/02/02(dd/mm/yy 或 yy/dd/mm 或 mm/yy) /dd) 都一样,对吧?并且逻辑应该能够匹配它并带入适当的通用最终格式。对吗?
  • 那么 02/03/2002 那么是 3 月 2 日还是 2 月 3 日呢?
  • 重点是……通过上面的正则表达式,你肯定会得到 3 个部分……这肯定取决于你……决定你想把什么逻辑放在上面确定日、月或年
  • 我同意。但应该有一些出路? :-(
  • 让我们忘记所有的正则表达式或一切......只是想简单我告诉你 02/03/01 但不告诉你日期格式......你从中得到什么?有一条出路,向您发送数据的一方可能知道格式......他们必须分享它......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多