第一种方法:
Using BASE R解决方案(不使用任何包):
pattern <- "\\d{2,4}[.-]\\d{2}[.-]\\d{2,4}"
rep <- c("On the evening of 2017-04-23, I was too tired","to complete my homework that was due on 24.04.2017.")
val <- unlist(regmatches(rep, gregexpr(pattern, rep)))
val1 <- as.Date(val,format=c("%Y-%m-%d","%d.%m.%Y"))
val2 <- format(val1,"%B %d, %Y")
val2
rep1 <- list()
for (i in 1:length(rep)){
rep1[i] <- gsub(pattern,val2[i],rep[i])
}
Answer:
do.call("c",rep1)
> do.call("c",rep1)
[1] "On the evening of April 23, 2017, I was too tired"
[2] "to complete my homework that was due on April 24, 2017."
>
第二种方法:
使用库stringr
library(stringr)
rep <- c("On the evening of 2017-04-23, I was too tired","to complete my homework that was due on 24.04.2017.")
val <- str_extract(rep,"\\d{2,4}[.-]\\d{2}[.-]\\d{2,4}")
val1 <- as.Date(val,format=c("%Y-%m-%d","%d.%m.%Y"))
val2 <- format(val1,"%B %d, %Y")
rep1 <- str_replace_all(rep,"\\d{2,4}[.-]\\d{2}[.-]\\d{2,4}",val2)
rep1
Answer:
> rep1
[1] "On the evening of April 23, 2017, I was too tired"
[2] "to complete my homework that was due on April 24, 2017."
>
编辑OP稍微改变了问题之后,解决方案更通用,但是假设月份始终位于中间,并且分隔符仅限于破折号(-)和点(.):
pattern <- "\\d{2,4}[.-]\\d{2}[.-]\\d{2,4}"
rep <- c("On the evening of 2017-04-23, I was too tired","to complete my homework that was due on 24.04.2017.")
val <- unlist(regmatches(rep, gregexpr(pattern, rep)))
year <- regmatches(val, gregexpr("\\d{4}", val))
month <- regmatches(val, gregexpr("(?<=[.-])\\d{1,2}(?=[.-])", val,perl=T))
date <- regmatches(val, gregexpr("(?<=[.-])\\d{2}$|^\\d{2}(?=[.-])", val,perl=T))
#Extracting year month and date , assuming month always falls in middle string
date1 <- paste0(year,"-",month,"-",date)
date1 <- as.Date(date1,"%Y-%m-%d")
val2 <- format(date1,"%B %d, %Y")
rep1 <- list()
for (i in 1:length(rep)){
rep1[i] <- gsub(pattern,val2[i],rep[i])
}
do.call("c",rep1)