Stringr 模式环顾四周:R

Stringr pattern look around: R

我正在尝试提取与特定模式匹配的子字符串。例如:

str <- "For each of the following statements, please indicate how true it is for\r\nyou with respect to your interaction with the puzzles in the game. - This is the part of string I want to extract."
str_extract(str, pattern = "(?<=-)\w+") #Output = This

如何获取整个字符串?

如果我们想提取整个句子直到 .,匹配一个或多个不是 . ([^.]+) 后跟 [=11= 的字符](\. - 转义,因为它是匹配任何字符的元字符)在正则表达式环视之后匹配 - 和 space (\s)

library(stringr)
str_extract(str, pattern = "(?<=-\s)[^.]+\.")
[1] "This is the part of string I want to extract."