用R从文本中提取日期

用R从文本中提取日期,r,regex,gsub,R,Regex,Gsub,我的数据框看起来像 df <- setNames(data.frame(c("2 June 2004, 5 words, ()(","profit, Insight, 2 May 2004, 188 words, reports, by ()("), stringsAsFactors = F), "split") 但是我的示例不起作用,感谢您一如既往的帮助因为只有一列,我们可以在提取列后直接使用gsub/sub。在模式中,日期可以是1天或更多,类似地,单词有3个('May')或4个字符

我的数据框看起来像

df <- setNames(data.frame(c("2 June 2004, 5 words, ()(","profit, Insight, 2 May 2004, 188 words,  reports, by ()("), stringsAsFactors = F), "split")

但是我的示例不起作用,感谢您一如既往的帮助

因为只有一列,我们可以在提取列后直接使用
gsub/sub
。在模式中,日期可以是1天或更多,类似地,单词有3个('May')或4个字符('June'),因此我们需要进行这些更改

sub(".*\\b(\\d{1,} \\w{3,4} \\d{4}).*", "\\1", df$split)
#[1] "2 June 2004" "2 May 2004" 
sub(".*\\b(\\d{1,} \\w{3,4} \\d{4}).*", "\\1", df$split)
#[1] "2 June 2004" "2 May 2004"