我编写了代码来从给定的字符串中提取日期。给定
> "Date: 2012-07-29, 12:59AM PDT"
它提取
> "2012-07-29"
问题是我的代码看起来很长而且阅读起来很麻烦。我想知道这样做是否是一种更优雅的方式。
raw_date = "Date: 2012-07-29, 12:59AM PDT"
#extract the string from raw date
index = regexpr("[0-9]{4}-[0-9]{2}-[0-9]{2}", raw_date) #returns 'start' and 'end' to be used in substring
start = index #start represents the character position 's'. start+1 represents '='
end = attr(index, "match.length")+start-1
date = substr(raw_date,start,end); date