如何在n
中的此字符串中的colon
之前找到R
个字词?我使用stringr
但是会优先使用正则表达式。
Input on income economic activities: Small business, self-emp…
谢谢, 即
答案 0 :(得分:1)
Base R解决方案:
n <- 4
pattern <- paste0("(\\w+ ?){", n, "}(?=:)")
regmatches(s, regexpr(pattern, s, perl = T))
[1] "on income economic activities"
答案 1 :(得分:0)
执行:
New Size(10,20,30)
替换str_extract(str, "(\\w+ ?){3}:")
[1] "income economic activities:"
3