vec <- c("Dinner on Monday, tuesday, Wed, or Tursday?",
"SundayMonday",
"Are we meeting today?")
# a generic way of finding locale-friendly weekdays
wdays <- weekdays(Sys.Date() + 0:6)
wdays <- paste(wdays, collapse = "|")
wdays
# [1] "Thursday|Friday|Saturday|Sunday|Monday|Tuesday|Wednesday"
lapply(
regmatches(vec, gregexpr(wdays, vec, ignore.case = TRUE)),
tools::toTitleCase
)
# [[1]]
# [1] "Monday" "Tuesday"
# [[2]]
# [1] "Sunday" "Monday"
# [[3]]
# list()
Другой вариант (используя first days
выше, вектор длины 7).
wdays <- weekdays(Sys.Date() + 0:6)
lapply(vec, function(v) wdays[sapply(wdays, grepl, x = v, ignore.case = TRUE)])
# [[1]]
# [1] "Monday" "Tuesday"
# [[2]]
# [1] "Sunday" "Monday"
# [[3]]
# character(0)
# also
lapply(vec, function(v) Filter(function(w) grepl(w, v, ignore.case = TRUE), wdays))