[英]Extract dates from R script
I have a long R script that I need to extract many dates from.我有一个很长的 R 脚本,我需要从中提取许多日期。 I'm treating the script as a text file, and am trying to find the dates with regex look around functions.
我将脚本视为文本文件,并尝试使用正则表达式环视函数查找日期。
Here is an example of a chunk of code containing dates:下面是一段包含日期的代码示例:
Chamber1_3 <- subset(Chamber1Exp, Chamber1Exp$RealTime>=as.POSIXct("2019-05-21 06:01:45") &
Chamber1Exp$RealTime<= as.POSIXct("2019-05-21 06:23:58"))
plot(Chamber1_3$RealTime[Chamber1_3$Status=="PRE"],Chamber1_3$N2O_ppm[Chamber1_3$Status=="PRE"],
xlim=as.POSIXct(c("2019-05-21 06:01:45", "2019-05-21 06:23:58")),
ylim=c(.34,.35))
I want to recover "2019-05-21 06:01:45" and ""2019-05-21 06:23:58". They are repeated twice in the code, I just want them once.我想恢复“2019-05-21 06:01:45”和“2019-05-21 06:23:58”。它们在代码中重复了两次,我只想要它们一次。
I'm testing RegEx snippets in the RegExplain add-in to RStudio.我正在 RStudio 的 RegExplain 加载项中测试 RegEx 片段。 I was trying to use a look-around function to capture the dates following the common text
'as.POSIXct('
我试图使用环视功能来捕获常见文本
'as.POSIXct('
I thought this should work but I get nothing.我认为这应该可行,但我什么也没得到。
(?<=Xct\(\")(?=\")
Suggestions?建议?
We can extract all the 4 numbers- 2 numbers- 2 numbers one or more spaces 2 numbers:2 numbers: 2numbers, then unlist and take only the unique values.我们可以提取所有 4 个数字 - 2 个数字 - 2 个数字一个或多个空格 2 个数字:2 个数字:2 个数字,然后取消列出并只取唯一值。
your_vec <- c('Chamber1_3 <- subset(Chamber1Exp, Chamber1Exp$RealTime>=as.POSIXct("2019-05-21 06:01:45") &
Chamber1Exp$RealTime<= as.POSIXct("2019-05-21 06:23:58"))
plot(Chamber1_3$RealTime[Chamber1_3$Status=="PRE"],Chamber1_3$N2O_ppm[Chamber1_3$Status=="PRE"],
xlim=as.POSIXct(c("2019-05-21 06:01:45", "2019-05-21 06:23:58")),
ylim=c(.34,.35))')
unique(unlist(str_extract_all(your_vec, '[0-9]{4}-[0-9]{2}-[0-9]{2}\\s+[0-9]{2}:[0-9]{2}:[0-9]{2}')))
leaving:
[1] "2019-05-21 06:01:45"
[2] "2019-05-21 06:23:58"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.