正则表达式 R 回顾

Regex R lookbehind

如何在 R 中找到此字符串中 colon 之前的 n 个单词?我正在使用 stringr,但首选正则表达式。

Input on income economic activities: Small business, self-emp…

谢谢, E.

做:

str_extract(str, "(\w+ ?){3}:")

[1] "income economic activities:"

3 替换为您的 n

基础 R 解决方案:

n <- 4
pattern <- paste0("(\w+ ?){", n, "}(?=:)")
regmatches(s, regexpr(pattern, s, perl = T))

[1] "on income economic activities"