我想将字符串中存在的时间(以及AM / PM)提取到一个列表中,该列表可以呈现到selectizeInput中。我浏览了网上其他可用的解决方案,但是解决方案太复杂而难以理解或无法再现。我很高兴有人能建议这种简单的出路。
strg <- 'At 03:00 AM, 06:00 AM and 11:00 AM, only on Monday, Tuesday, Wednesday, Thursday, and Friday'
预期结果为时间列表:("03:00 AM","06:00 AM","11:00 AM")
答案 0 :(得分:3)
尝试将regmatches
与模式\b\d{2}:\d{2} (?:AM|PM)\b
一起使用:
strg <- 'At 03:00 AM, 06:00 AM and 11:00 AM, only on Monday, Tuesday, Wednesday, Thursday, and Friday'
m <- gregexpr("\\b\\d{2}:\\d{2} (?:AM|PM)\\b", strg)
regmatches(strg, m)[[1]]
[1] "03:00 AM" "06:00 AM" "11:00 AM"
答案 1 :(得分:0)
带有str_extract
library(stringr)
str_extract_all(strg, "[0-9]{2}:[0-9]{2} [AP]M")[[1]]
#[1] "03:00 AM" "06:00 AM" "11:00 AM"