且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用R从文本中提取日期

更新时间:2023-02-18 09:18:52

由于只有一列,因此我们在提取列之后可以直接使用gsub/sub.在模式中,日期可以是1个或更多,类似地,单词具有3个("5月")或4个字符("6月"),因此我们需要进行这些更改

As there is only a single column, we can directly use gsub/sub after extracting the column. In the pattern, the days can be 1 or more, similarly the words have 3 ('May') or 4 characters ('June'), so we need to make those changes

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