Regex R lookbehind

时间:2018-03-31 21:13:02

标签: r regex stringr

如何在n中的此字符串中的colon之前找到R个字词?我使用stringr但是会​​优先使用正则表达式。

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

谢谢, 即

2 个答案:

答案 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