从字符串向量中提取日期

时间:2016-05-03 02:08:07

标签: r string parsing vector extraction

我有两个元素的向量。每个元素都包含一串字符 有两套日期。我需要提取这两个日期的后者, 并用它们制作一个新的矢量或列表。

#webextract vector
webextract <- list("The Employment Situation, December 2006       January  5  \t 8:30 am\r","The Employment Situation, January 2007        \tFeb.  2, 2007\t 8:30 am            \r") 

#This is how the output of webextract looks like:
[[1]]
[1] The Employment Situation, December 2006       January  5  \t 8:30 am\r

[[2]]
[1] The Employment Situation, January 2007        \tFeb.  2, 2007\t 8:30 am            \r

webextract是网页抓取纯文字网址的结果,这就是为什么它看起来像那样。我需要提取的是“1月5日”和“2月2日”。我一直在试验grepstrsplit并且无法获得任何结果。已经完成所有相关的SO问题而没有成功。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

我们可以在gsub'webextract'后尝试使用unlist

gsub("^\\D+\\d+\\s+|(,\\s+\\d+)*\\D+\\d+:.*$", "", unlist(webextract))
#[1] "January  5" "Feb.  2"   
相关问题